Graphing from txt file? - Homework
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I am not sure what is wrong with the script:
So the data looks like this:
World population
YEAR BC/AD POPULATION
10000 BC 1000000
8000 BC 5000000
6500 BC 5000000
5000 BC 5000000
I need to use the first column (year) as the x axis (as negative numbers if BC and positive is AD).
I need the third column to be the y axis in log scale (I am having trouble with this part too).
My script so far:
[fid, msg] = fopen('hw12_2.txt','w');
if fid==-1
fprintf(2,'FAILED TO OPEN FILE. %s\n', msg);
return;
end
% FIRST LINE OF DATA
title_str= fgetl(fid);
% SECOND LINE HAS LABELS
aline=fgetl(fid);
[x_str, ystr]=strtok(aline);
y_str = ystr(7:end);
hold on
line1 = fgetl(fid);
line2 = fgetl(fid);
y = fid(:,3);
x = fid(:,1);
plot(x,y);
% USE LABELS FROM ABOVE USING FIRST AND SECOND LINE
xlabel(x_str);
ylabel(y_str);
title(title_str);
hold off
採用された回答
Umair Nadeem
2013 年 11 月 17 日
編集済み: Umair Nadeem
2013 年 11 月 17 日
I get it what you are trying to achieve. Here is the code I developed after a little modification of yours.
clear all;
clc;
[fid, msg] = fopen('hw12_2.txt','r');
if fid==-1
fprintf(2,'FAILED TO OPEN FILE. %s\n', msg);
return;
end
% FIRST LINE OF DATA
title_str= fgetl(fid);
% Initialize z and y axis arrays
xstr =[];
ystr =[];
aline = 1;
while aline ~= -1
% SECOND LINE HAS LABELS
aline=fgetl(fid);
if (aline ~= -1)
[x_str, y_str_temp] = strtok(aline);
[Dec, y_str] = strtok(y_str_temp);
x_str = str2double(x_str);
y_str = str2double(y_str);
% cmpr stores the result of comparison of both strings i.e. check
% whether it is equal to BC or not
cmpr = strcmp(Dec, 'BC');
if (cmpr == 1)
x_str = -1 * x_str;
end
xstr = [xstr x_str];
ystr = [ystr y_str];
end
end
% Convert the y-axis in log scale
ystr = log10(ystr);
bar(xstr,ystr);
% USE LABELS FROM ABOVE USING FIRST AND SECOND LINE
xlabel('Year');
ylabel('Population');
title(title_str);
It works perfectly fine and just like the way you want it, but you have to take care of one thing that the text file must not have any empty lines between the line with actual text, otherwise the compiler would take that the file has ended and it will return a -1. It should be like this
YEAR BC/AD POPULATION
10000 BC 1000000
8000 BC 5000000
6500 BC 5000000
5000 BC 5000000
This way it will work absolutely fine. Hope it helps
7 件のコメント
If you want the Y scale to be logarithmic (without transforming the data to log10), you can use:
set(gca,'YScale','log')
before plot, bar or hold on
Umair Nadeem
2013 年 11 月 17 日
Ok now it would work with a file with alternate empty lines also :)
clear all;
clc;
[fid, msg] = fopen('hw12_2.txt','r');
if fid==-1
fprintf(2,'FAILED TO OPEN FILE. %s\n', msg);
return;
end
% FIRST LINE OF DATA
title_str = fgetl(fid);
% Dump the result for an empty line
dump = fgetl(fid);
% SECOND LINE HAS LABELS
line_2 = fgetl(fid);
% Initialize z and y axis arrays
xstr =[];
ystr =[];
aline = 1;
while aline ~= -1
% Dump the result for an empty line
dump2 = fgetl(fid);
% ALL DATA LINES
aline=fgetl(fid);
if (aline ~= -1)
[x_str, y_str_temp] = strtok(aline);
[Dec, y_str] = strtok(y_str_temp);
x_str = str2double(x_str);
y_str = str2double(y_str);
% cmpr stores the result of comparison of both strings
% i.e. check
% whether it is equal to BC or not
cmpr = strcmp(Dec, 'BC');
if (cmpr == 1)
x_str = -1 * x_str;
end
xstr = [xstr x_str];
ystr = [ystr y_str];
end
end
% Convert the y-axis in log scale
ystr = log10(ystr);
bar(xstr,ystr);
% USE LABELS FROM ABOVE USING FIRST AND SECOND LINE
xlabel('Year');
ylabel('Population');
title(title_str);
It would work with this type of file with empty lines in between
World population
YEAR BC/AD POPULATION
10000 BC 1000000
8000 BC 5000000
6500 BC 5000000
5000 BC 5000000
Nora
2013 年 11 月 17 日
This helped a bit. The data actually doesn't have spaces inbetween each line. And I am still having trouble with the graphing portion though.
Image Analyst
2013 年 11 月 17 日
編集済み: Image Analyst
2013 年 11 月 17 日
You forgot to post your data file. How can they properly write a program if they're just guessing (wrong apparently) at how your data is formatted? Use the paper clip icon and don't forget to click the "Attach File" button after you browsed to your file.
Nora
2013 年 11 月 18 日
The data isn't showing on the graph. There is a blank graph with the script.
[fid, msg] = fopen('hw12_2.txt','r');
if fid==-1
fprintf(2,'FAILED TO OPEN FILE. %s\n', msg);
return;
end
% FIRST LINE OF DATA
title_str = fgetl(fid);
% Dump the result for an empty line
dump = fgetl(fid);
% SECOND LINE HAS LABELS
line_2 = fgetl(fid);
% Initialize z and y axis arrays
xstr =[];
ystr =[];
aline = 1;
while aline ~= -1
% Dump the result for an empty line
dump2 = fgetl(fid);
% ALL DATA LINES
aline=fgetl(fid);
if (aline ~= -1)
[x_str, y_str_temp] = strtok(aline);
[Dec, y_str] = strtok(y_str_temp);
x_str = str2double(x_str);
y_str = str2double(y_str);
% cmpr stores the result of comparison of both strings
% i.e. check
% whether it is equal to BC or not
cmpr = strcmp(Dec, 'BC');
if (cmpr == 1)
x_str = -1 * x_str;
end
xstr = [xstr x_str];
ystr = [ystr y_str];
end
end
% Convert the y-axis in log scale
ystr = log10(ystr);
semilogy(ystr);
% USE LABELS FROM ABOVE USING FIRST AND SECOND LINE
xlabel('Year');
ylabel('Population');
title(title_str);
???
Image Analyst
2013 年 11 月 18 日
Looks like hw12_2.txt somehow didn't get attached. Try again. Make sure you click the "Attach file" button after you click the "Choose file" button.
Nora
2013 年 11 月 18 日
I found out the problem. Thank you!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Whos についてさらに検索
参考
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)
