필터 지우기
필터 지우기

Getting variables for different files

조회 수: 9 (최근 30일)
Madlab
Madlab 2018년 10월 16일
편집: Stephen23 2018년 10월 17일
I am trying to load multiple files and extract respective data from them. I have managed to successfully load the multiple files, but I am having trouble assigning each unique variable to them. I have uploaded 2 sample files, contained within a folder.
This means that I manage to loop through for the different areas, but each time the loop processes, the variables A,B,C changes.
I want it such that A1,B1,C1 is for the first area, A2,B2,C2 is for the second area and so on...how do I go about doing this? I tried A(k),B(k)C(k) but it did not work.
inFile = 'filepath\weather\';
location={'savannah-ga_temp','newyork-ny_temp','acadia-me_temp','boston-ma_temp','charleston-sc_temp','kingston-ri_temp'};
for k=1:length(location)
loc = strcat(location(k), '.csv');
tempfile = string(loc)
combinename = strcat (inFile,tempfile)
% Loading file
[A,B,C] = textread(combinename,'%*s %f %f %*f %f','headerlines',2,'delimiter',',','emptyvalue',NaN);
end
  댓글 수: 1
Stephen23
Stephen23 2018년 10월 16일
편집: Stephen23 2018년 10월 16일
"I want it such that A1,B1,C1 is for the first area, A2,B2,C2 is for the second area and so on..."
Magically changing/accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code. Read this to know why:
You should just use indexing and some cell arrays. Indexing is simple, neat, and very efficient. Unlike what you are trying to do. Indexing is exactly what the MATLAB documentation shows:

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 10월 16일
편집: Stephen23 2018년 10월 16일
Just use cell arrays:
N = numel(location);
A = cell(1,N);
B = cell(1,N);
C = cell(1,N);
for k = 1:N
[A{k},B{k},C{k}] = textread(...);
end
Note that the textscan documentation states clearly: " textread is not recommended. Use textscan instead."
  댓글 수: 2
Stephen23
Stephen23 2018년 10월 17일
Madlab's "Answer" moved here:
Hi Stephen, thank you very much for your input. That was very helpful. I am using textscan to open a txt file now, but I am unable to extract out the relevant data.
inFile = 'filepath\level\';
location={'fort-pulaski-ga_tg','newyork-ny_tg','barharbor-me_tg','boston-ma_tg','charleston-sc_tg','newport-ri_tg'};
N = numel(location);
A = cell(1,Nloc);
B = cell(1,Nloc);
for k=1:length(location)
loc = strcat(location(k), '.txt');
tempfile = string(loc)
combinename = strcat (inFile,tempfile)
[A{k},B{k}] = textscan(combinename,'%f %f','delimiter',';','emptyvalue',NaN);
end
I have tried
[A{k}] = textscan(combinename,'%f %f','delimiter',';','emptyvalue',NaN);
but the output still does not show me the data.
In the files, there are data lined up like this
1935.041600 ; 7005
1935.125000 ; 6898
1935.208400 ; 6868
1935.291600 ; 6993
1935.375000 ; 6980
1935.458400 ; 6947
Where the first part is decimal years and the second part, after the ";" is the level. May I know where I am going wrong?
edit : the extracted data shows a 1 by x cell, but there are no values inside.
Stephen23
Stephen23 2018년 10월 17일
편집: Stephen23 2018년 10월 17일
@Madlab: this will read the supplied file (test file is attached):
D = '.'; % directory where the files are saved.
F = 'fort-pulaski-ga_tg.txt';
opt = {'Delimiter',';', 'CollectOutput',true};
str = fileread(fullfile(D,F));
str = strrep(str,' ','');
C = textscan(str,'%f%f',opt{:});
M = C{1}
You can put it into your loop something like this (untested):
opt = {'Delimiter',';', 'CollectOutput',true};
D = '.'; % directory where the files are saved.
L = {'fort-pulaski-ga_tg', 'newyork-ny_tg', 'barharbor-me_tg', 'boston-ma_tg', 'charleston-sc_tg', 'newport-ri_tg'};
N = numel(L);
C = cell(1,N);
for k = 1:N
F = fullfile(D,sprintf('%s.xt',L{k}));
str = fileread(F);
str = strrep(str,' ','');
C(k) = textscan(str,'%f%f',opt{:});
end % note C(k) uses parentheses!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by