plotting multiple series on one graph from a text file
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
i am trying to make a single graph from a text file which is looped with 15 different variables over time.
does anyone know how i can do this? below is an attachment of the text file im attempting to graph.
column one repeats itself 1-15 and i wish for each integer from 1-15 to have its own series on the graph
採用された回答
Star Strider
2019 年 2 月 11 日
They all appear to plot together, and there is no easy way to offset them to plot them in a single plot. The best option then appears to be plotting them each in a subplot:
fidi = fopen('carpos.txt','rt');
Dc = textscan(fidi, '%f%f%f', 'CollectOutput',1);
D = cell2mat(Dc);
endData = find(D(:,1) == 14, 1, 'last');
D = D(1:endData,:);
Dr = reshape(D, 15, 3, []);
figure
for k1 = 1:size(Dr,1)
subplot(5,3,k1)
plot3(squeeze(Dr(k1,1,:)), squeeze(Dr(k1,2,:)), squeeze(Dr(k1,3,:)))
grid on
axis equal
title(sprintf('%d',k1-1))
end
Experiment to get the result you want.
7 件のコメント
You didn’t originally say how you wanted them plotted. I took my best shot.
I needed to restate the reshape call to calculate ‘Dr’ to do what you want.
The revised full code is now:
fidi = fopen('carpos.txt','rt');
Dc = textscan(fidi, '%f%f%f', 'CollectOutput',1);
D = cell2mat(Dc);
endData = find(D(:,1) == 14, 1, 'last');
D = D(1:endData,:);
Dr = reshape(D, 15, [], 3);
figure
hold all
for k1 = 1:size(Dr,1)
plot(squeeze(Dr(k1,:,2)), squeeze(Dr(k1,:,3)))
end
hold off
grid
legend(compose('%2d',0:14), 'Location','NW')
xlabel('Simulation Time (10^{-1} s)')
title('Car Position')
legend(compose('%2d',0:14), 'Location','NW')
Use the sprintfc (undocumented) function if you do not have compose. The arguments are the same.
Experiment to get the result you want.
Star Strider
2019 年 2 月 11 日
編集済み: Star Strider
2019 年 2 月 11 日
I tested this revision with your new file.
With two experiments, the ‘Dr’ assignment sould be:
Dr = reshape(D, 2, [], 3);
To automate it so that my code will automatically scale for any number of experiments, substitute these lines for the one ‘Dr’ assignment:
NrExpts = unique(D(:,1));
Dr = reshape(D, numel(NrExpts), [], 3);
The rest of my code automatically uses the size of ‘Dr’ to change the plot and legend entries.
The revised code is now:
fidi = fopen('carpos(2).txt','rt');
Dc = textscan(fidi, '%f%f%f', 'CollectOutput',1);
fclose(fidi);
D = cell2mat(Dc);
NrExpts = unique(D(:,1));
Dr = reshape(D, numel(NrExpts), [], 3);
figure
hold all
for k1 = 1:size(Dr,1)
plot(squeeze(Dr(k1,:,2)), squeeze(Dr(k1,:,3)))
end
hold off
grid
xlabel('Simulation Time (10^{-1} s)')
title('Car Position')
legend(compose('%2d',0:14), 'Location','NW')
If my Answer helped you solve your problem, please Accept it!
Some of your files do not have the complete set of experiments. My code (different versions) ran without error with the files you posted.
This revision should work for all your files, although it will not use the ending data sets in your files that do not have the complete set of experiments, because those are incompatible with the reshape function.
The revised code is now:
fidi = fopen('carpos.txt','rt');
Dc = textscan(fidi, '%f%f%f', 'CollectOutput',1);
fclose(fidi);
D = cell2mat(Dc);
ExptVct = unique(D(:,1)); % ‘Experiments’ Vector (Assumes All Data Sets Have The Same Number Of Experiments)
RowLim = floor(size(D,1)/numel(ExptVct))*numel(ExptVct); % Row Limit Of Data Sets With Complete Experiments
D = D(1:RowLim,:); % Trim ‘D’ To Data Sets With Complete Experiments
Dr = reshape(D, numel(ExptVct), [], 3);
figure
hold all
for k1 = 1:size(Dr,1)
plot(squeeze(Dr(k1,:,2)), squeeze(Dr(k1,:,3)))
end
hold off
grid
xlabel('Simulation Time (10^{-1} s)')
title('Car Position')
legend(compose('%2d',ExptVct), 'Location','NW')
I tested this with both ‘carpos(1).txt’ (the original file) and ‘carpos(2).txt’ the second file you provided. It now also uses ‘ExptVct’ for the legend in the plot.
You could turn this into a function if you want, with the only argument being the file name. That would be:
function carposplot(filename)
fidi = fopen(filename,'rt');
Dc = textscan(fidi, '%f%f%f', 'CollectOutput',1);
fclose(fidi);
D = cell2mat(Dc);
ExptVct = unique(D(:,1)); % ‘Experiments’ Vector (Assumes All Data Sets Have The Same Number Of Experiments)
RowLim = floor(size(D,1)/numel(ExptVct))*numel(ExptVct); % Row Limit Of Data Sets With Complete Experiments
D = D(1:RowLim,:); % Trim ‘D’ To Data Sets With Complete Experiments
Dr = reshape(D, numel(ExptVct), [], 3);
figure
hold all
for k1 = 1:size(Dr,1)
plot(squeeze(Dr(k1,:,2)), squeeze(Dr(k1,:,3)))
end
hold off
grid
xlabel('Simulation Time (10^{-1} s)')
title('Car Position')
legend(compose('%2d',ExptVct), 'Location','NW')
end
Save it as ‘carposplot.m’ to a directory in your MATLAB search path. Then call it as:
carposplot('carpos.txt')
It has no outputs, although you could return some of its calculations if you want, by defining the function to have those outputs.
I tested the function with both of your files, and it ran without error with each.
ciaran balfe
2019 年 2 月 13 日
perfect that works thank you !
Star Strider
2019 年 2 月 13 日
My pleasure.
If my Answer helped you solve your problem please Accept it!
You need to tell textscan to ignore the
column (since I believe that is what you want to do). Do that by adding a ‘%*f’ to the end of the format string:
fidi = fopen('pos2.txt');
Dc = textscan(fidi, '%f%f%f%*f', 'CollectOutput',1);
fclose(fidi);
D = cell2mat(Dc);
ExptVct = unique(D(:,1)); % ‘Experiments’ Vector (Assumes All Data Sets Have The Same Number Of Experiments)
RowLim = floor(size(D,1)/numel(ExptVct))*numel(ExptVct); % Row Limit Of Data Sets With Complete Experiments
D = D(1:RowLim,:); % Trim ‘D’ To Data Sets With Complete Experiments
Dr = reshape(D, numel(ExptVct), [], 3);
figure
hold all
for k1 = 1:size(Dr,1)
plot(squeeze(Dr(k1,:,2)), squeeze(Dr(k1,:,3)))
end
hold off
grid
xlabel('Simulation Time (10^{-1} s)')
title('Car Position')
legend(compose('%2d',ExptVct), 'Location','NW')
producing:
%20-%202019%2002%2023.png)
This looks like the previous plots, so I’m guessing that I ignored the correct (
) column of repeated ‘2’ values.
Experiment to get the result you want.
ciaran balfe
2019 年 3 月 11 日
hi thank you again for your help, i hope you dont mind me asking another question. i have 2 output files and the are crossing over in data. i basically am looking for the exact same graph as you have kindly shown me beofre but it isnt graphing properly. ive attached a text file im trying to graph. thank you again. (fyi you were completely correct about ignoring the 4th column, its to be ignored in this one too).
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で MATLAB についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
