Borrar filtros
Borrar filtros

How to save each loop data

8 visualizaciones (últimos 30 días)
Chuanjung Lin
Chuanjung Lin el 22 de Oct. de 2019
Comentada: Chuanjung Lin el 30 de Oct. de 2019
Hi all,
I have a batch file need to input to workspace for analysis, and the file type is *.csv.
Following is the code that I used, and the output file only show thw last one "P"
I hope each input file cas save as P1, P2, P3,....
May I know how to modify it ? thank you.
%% Open file dir
[filename,filedir] = uigetfile('*.csv','Multiselect','on');
path = fullfile(filedir,filename);
path = path';
file = struct('name',path);
%% Cover struc to cell
c= struct2cell(file);
opts = delimitedTextImportOptions("NumVariables", 9);
% Specify range and delimiter
opts.DataLines = [3, Inf];
opts.Delimiter = [" ", ","];
% Specify column names and types
opts.VariableNames = ["V", "I1V", "I2V", "I3V", "I4V", "I5V", "I6V", "I7V", "I8V"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
%% Readtable Import the data
for i=1:length(file)
P = table2array(readtable(c{i}, opts));
end
  1 comentario
Stephen23
Stephen23 el 22 de Oct. de 2019
Editada: Stephen23 el 22 de Oct. de 2019
"I hope each input file cas save as P1, P2, P3,...."
Don't do that. Read this to know why:
You should use indexing, just like the MATLAB documentation shows:
Indexing is simple, easy to debug, and very efficient (unlike what you are trying to do).

Iniciar sesión para comentar.

Respuesta aceptada

Subhadeep Koley
Subhadeep Koley el 29 de Oct. de 2019
You defined P as a single variable, therefore it is holding only the last value of the loop. If you want to want to save each loop data to a separate variable, the following code might help you.
Just declare P as an empty cell array and access P using the indexing variable i in every loop.
%% Open file dir
[filename,filedir] = uigetfile('*.csv','Multiselect','on');
path = fullfile(filedir,filename);
path = path';
file = struct('name',path);
%% Cover struc to cell
c= struct2cell(file);
opts = delimitedTextImportOptions("NumVariables", 9);
% Specify range and delimiter
opts.DataLines = [3, Inf];
opts.Delimiter = [" ", ","];
% Specify column names and types
opts.VariableNames = ["V", "I1V", "I2V", "I3V", "I4V", "I5V", "I6V", "I7V", "I8V"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Declare empty cell array P
P = {};
%% Readtable Import the data
for i=1:length(file)
P{i} = table2array(readtable(c{i}, opts));
end
Hope this helps!
  2 comentarios
Stephen23
Stephen23 el 29 de Oct. de 2019
+1 tidy answer.
Using path as a variable name should be avoided, as this shadows the inbuilt path function.
Chuanjung Lin
Chuanjung Lin el 30 de Oct. de 2019
Thank you for your help^_^!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by