フィルターのクリア

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 Center および File ExchangeText Data Preparation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by