how to load multiple files and do the computation for each of them and write the output in consequent rows

1 次查看(过去 30 天)
i have a list of engine data files and im supposed to automate the process of decoding them and finding the average and etc. the average output is supposed to be directly written to an excel sheet. this process is to be done for all the files in the folder.
the codes i used are the following:
to read multiple files
numfiles = 6;
mydata = cell(1,numfiles);
for k = 1:numfiles
myfilename = dlmread(sprintf('test%d.xlsx',k));
mydata{k} = importdata(myfilename);
end
to compute
T.message=categorical(T.message);
B = T(T.message=='18FF0803x',:);
columns = B(:,7:end);
%accessing the first column
engine_temperature = columns{:,1};
engine_temperature = hex2dec(engine_temperature);
engine_temperature(:,1) = 1*engine_temperature(:,1) - 40;
mean_engine_temperature = mean(engine_temperature);
%accessing the second column
engine_oil_pressure = columns{:,2};
engine_oil_pressure = hex2dec(engine_oil_pressure);
engine_oil_pressure(:,1) = 0.02*engine_oil_pressure(:,1);
mean_engine_pressure = mean(engine_oil_pressure);
to write the output to the excel sheet
writecell(mean_engine_temperature,filename,'Sheet','Temperatures','Range','B');
can anyone please help me to put this code in a loop such that all the files are opened and decoded one after the other and the mean temperature and pressure values are printed in consequent rows and columns.

采纳的回答

Jakob B. Nielsen
Jakob B. Nielsen 2020-2-24
Your loop will want to start with grabbing all files in a folder. uigetfile with the MultiSelect option will work wonders here:
[Name,Path]=uigetfile({'*.xlsx*'},'MultiSelect','on');
entirefile=fullfile(Paths,Names);
Then start your loop. It wants to run a number of times equal to the number of elements in your entirefile cell, as you also have in your own code. Within this loop, do all your loading, calculation and writing. To avoid cluttering the workspace, you might want to look into a structure and dot indexing.
for i=1:numel(entirefile);
%your entire code including
enginedata(i).raw=importdata(entirefile{i});
enginedata(i).temperature = your temperature calculation
enginedata(i).oilpressure = oil pressure calculation
%and so on
%finally, to write in consecutive fields the results, simply keep indexing your things:
celltarget=['B',num2str(i)]; %B1 first run, B2 2nd run etc.
writecell(mean_engine_temperature,filename,'Sheet','Temperatures','Range',celltarget);
end
  5 个评论
Jakob B. Nielsen
Jakob B. Nielsen 2020-2-26
Ah right. I think you can loop it in like this;
for j=1:size(columns,1)
data(i).temperature(j) = mean((hex2dec(columns{j,1}))-40);
end
that way you give hex2dec just one input at a time. Although your column there contains a bunch of 60 entries which is not a valid input for hex2dec either, so you will need to fix that somehow, as well.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by