Plotting multiple text files columns in the same graph

8 ビュー (過去 30 日間)
Gabriel Maya
Gabriel Maya 2016 年 3 月 6 日
コメント済み: Gabriel Maya 2016 年 3 月 7 日
Hi guys, I have about 25 files consisting of 300 rows and 3 columns.
I'd like to plot the first column of each file in the same graph. Is it possible?
Thanks in advance !
  3 件のコメント
Gabriel Maya
Gabriel Maya 2016 年 3 月 6 日
編集済み: Walter Roberson 2016 年 3 月 6 日
Hi Ced: here is the code I got so far:
workDir='C:\Users\gabri\Desktop\Nova_pasta\Arquivos_Saida_Imex';
for i=1:25
cd(workDir);
pasta= ['cd ' num2str(i) ];
eval (pasta)
filename= '' workDir ''\'' num2str(i) ''\''arqanepi.rwo''
delimiter = '\t';
startRow = 6;
formatSpec = '%f%f%f%f%[^\n\r]';
%%Open the text file.
fileID = fopen('arqanepi.rwo','r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Allocate imported array to column variable names
days(:,i) = dataArray{:, 1};
Oil(:,i) = dataArray{:, 2};
Gas(:,i) = dataArray{:, 3};
Water(:,i)= dataArray{:, 4};
cd (workDir)
end
plot(days(:,1), Oil(:,:))
I am able to save all my data in the Variables day, oil, water and gas.
But when I try to plot all curves of Oil, for example, I only get one.
Can you help me with this?
Best Regards.
Stephen23
Stephen23 2016 年 3 月 7 日
@Gabriel Maya: don't use cd to switch between files: this is slow and buggy. It is much more faster and more reliable to pass full/relative filepaths to your file handling functions, exactly as Walter Roberson shows in their answer. Also you should avoid using eval for such trivial code, which is also slow and buggy way to program.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 3 月 7 日
workDir = 'C:\Users\gabri\Desktop\Nova_pasta\Arquivos_Saida_Imex';
delimiter = '\t';
startRow = 6;
%your description says three columns, your code says four, your previous format implied their might be more
formatSpec = '%f%f%f%f';
for i = 1:25
this_subdir = sprintf('%d', i);
filename = fullfile( workDir, this_subdir, 'arqanepi.rwo');
%%Open the text file.
fileID = fopen(filename, 'rt');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines', startRow-1, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Allocate imported array to column variable names
days(:,i) = dataArray{:, 1};
Oil(:,i) = dataArray{:, 2};
Gas(:,i) = dataArray{:, 3};
Water(:,i)= dataArray{:, 4};
end
plot(days(:,1), Oil)
  2 件のコメント
Ced
Ced 2016 年 3 月 7 日
Did that answer your question? Otherwise, attach one of your files.
PS: If you want to have a legend for all files without having to type it up, you could do something like
N_files = 25;
leg_str = cellfun(@(x)sprintf('dataset %i',x),num2cell(1:N_files),'UniformOutput',0);
legend(leg_str)
Gabriel Maya
Gabriel Maya 2016 年 3 月 7 日
That answered !
Thanks for the sugestion also :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by