필터 지우기
필터 지우기

loading file stored as string and then plotting data from file

조회 수: 3 (최근 30일)
I am attempting to create a script which asks the user for the filename of a txt file whose contents will later be plotted.
filename = input('What is the filename (without the extension) e.g. RCP6: ','s');
if isempty(filename)
filename=input('What is the filename (without the extension) e.g. RCP6: ','s');
end
ext = input('What is the filetype (extension) e.g. .txt: ','s');
if isempty(ext)
ext=input('What is the filetype (extension) e.g. .txt: ','s');
end
filew = strcat(filename,ext)
load(filew)
A = filename
Y = A(:,1)
E = A(:,2)
plot(Y,E)
xlabel('calendar year')
ylabel('annual fossil carbon emissions (GtC)')
As written, the code correctly concatenates filename and ext, however, it does not appear that load (filew) correctly loads that file, since given a filename = RCP3PD for example, Y = R and E = C, instead of Y storing the first column of values from RCP3PD.txt, and E storing the second column?
Any Suggestions? I have seen other "load file from string" threads make reference to the sprintf() function - would that apply here? Perhaps having the user point to a particular directory would be a better approach?

채택된 답변

per isakson
per isakson 2012년 8월 9일
I propose you replace
load(filew)
A = filename
by
A = load( filew );
.
That makes the code easier to read and understand - I think.

추가 답변 (1개)

Sriram
Sriram 2012년 8월 9일
Yeah your code would give you as R and C....! If you are trying to access the content of the file you shd write as
Y = filew(:,1) % as you have loaded the file "filew"
What you have done would provide the first character of your filname ..And also check with the load command -- i hope it loads only the data files and not any other format...

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by