Graphing from txt file? - Homework
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
