-1

I have x-y data that exists in a text file, which looks like this:

(("height-0.3m")    
-0.0527942  0.0595315
-0.0525685  0.0594241
)

(("height-0.55m")   
-0.0527939  0.0263362
-0.0525683  0.0265083
)

(("height-0.83m")
-0.0528517  0.233493
-0.0526329  0.231228
)

I want to read this data in MATLAB and plot the x-y data on the same graph using the numbers in the tables titles for the plot's legend i.e. the 0.3m, 0.55m, etc.

The data is space-delimited and tables have arbitrary number of rows, but the tables are always contained in parenthesis and there is an empty line at the end of each table.

Can anyone help me on this issue?
Your help is much appreciated.
Best Regards,
Emma

Emma
  • 149
  • 10

1 Answers1

0

Here is code which achieves what you want and the resulting plot:

clear; clc;

%% Initialize variables.
filename = 'C:\Users\UserName\FileLocation\data.txt';
delimiter = {'((',' ',')'};

%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%q%q%[^\n\r]';

%% Open the text file.
fileID = fopen(filename,'r');

%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true,  'ReturnOnError', false);

%% Close the text file.
fclose(fileID);


data = repmat({''},length(dataArray{1}),2);
for col=1:length(dataArray)-1
    data(1:length(dataArray{col}),col) = dataArray{col};
end

% Plotting each height value
PlotMatrix = [];
leg = {};
for k = 1:length(data(:,2))
    if strcmp(data{k,1}(1:6),'height') 
        leg{end+1,1} = data{k,1};
        if ~isempty(PlotMatrix)
            figure(1)
            hold on
            plot(PlotMatrix(:,1), PlotMatrix(:,2))
            hold off
        end
        PlotMatrix = [];
    else
        PlotMatrix = [PlotMatrix; [str2double(data{k,1}), str2double(data{k,2})]];
    end
end

% Plot last matrix
if ~isempty(PlotMatrix)
    figure(1)
    hold on
    plot(PlotMatrix(:,1), PlotMatrix(:,2))
    hold off
end

legend(leg{:});

Plot from text array

Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • Thank you very much for your effort. The solution almost works, but I still have few issues that I could not solve by myself. First, the data seems to be tab-delimited not space-delimited (fault from using Excel :) Then, I actually have realized that table titles are separated into title for x-data and title for y-data, but in the sample I provided I modified it for simplification. So the first table in the original question looks exactly like this: `((xy/key/label "h-0.3m") -0.0527942 0.0595315 -0.0525685 0.0594241 )` – Emma Feb 07 '17 at 07:56
  • I suggest you look at the import tool. On the `Home` tab, click `Import Data`. You can play around with the settings, then on the `Import Selection` dropdown you can generate the code. This code can often be simplified so play around with what different lines do. As for the tab-delimiting, just add that to the delimiters list, `'\t'`? https://uk.mathworks.com/help/matlab/ref/textscan.html#input_argument_namevalue_Delimiter – Wolfie Feb 07 '17 at 08:15
  • Also, please just edit the question to add details when formatting starts getting complicated like your first comment, otherwise things quickly become unclear. – Wolfie Feb 07 '17 at 08:18
  • When I ran the code, the numbers in `dataArray` get attached to each other like: `-0.05279420.0595315` The output plot should be like the graph you provided, i.e. without the x-data heading, but with the y-data heading! – Emma Feb 07 '17 at 08:25
  • Are you using `str2double`? Are your running the code exactly as it is above but with your own file path? – Wolfie Feb 07 '17 at 08:27