For loop overriding pervious data

조회 수: 1(최근 30일)
Prasad Joshi
Prasad Joshi 2022년 1월 18일
댓글: Prasad Joshi 2022년 1월 19일
Hello I am loading all of my file but for loop is overriding the previous data...mean like for each file the data should be stored in excel file how should i implement it .....Like I want is file 1 data should be stored than file 2 and next file 3 in excel .......The below code i m using .....
clear all
close all
clc
filenames={'file1.mat,file2.mat,file3.mat'}
for i = 1:nume1(filenames)
load(filenames{i})
THC1= THC_sim_cum
end
xlswrite('e.xlsx',[THC1])
or any other alternative of loading the matlab data for all three files to excel is also fine...the no of files can increase it can be 20 also .....Thank u in advance

채택된 답변

Ankit
Ankit 2022년 1월 19일
I tweaked a bit @KSSV answer. Second loop I added in case you have different variable size in *.mat file. I hope this solution will work for you. It is good when you tell more about *.mat files and frame your question properly. It will avoid lot of effort. All the best.
filenames={'file1.mat','file2.mat','file3.mat'};
n = numel(filenames) ;
A = zeros([],n) ;
for i = 1:numel(filenames)
load(filenames{i})
THC1= THC_sim_cum;
for j = 1:length(THC1)
A(j,i) = THC1(j);
end
end
xlswrite('e.xlsx',A)
  댓글 수: 1
Prasad Joshi
Prasad Joshi 2022년 1월 19일
Thank you so much ankit the code work

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

추가 답변(2개)

KSSV
KSSV 2022년 1월 18일
filenames={'file1.mat,file2.mat,file3.mat'}
for i = 1:nume1(filenames)
load(filenames{i})
THC1= THC_sim_cum ;
[filepath,name,ext] = fileparts(filenames{i}) ;
fname = [name,'.xlsx'] ;
xlswrite(fname,THC1)
end
  댓글 수: 1
Prasad Joshi
Prasad Joshi 2022년 1월 19일
Thank you KSSV

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


KSSV
KSSV 2022년 1월 18일
If all the files have same dimensions, better save them into a matrix and then write into a file.
filenames={'file1.mat,file2.mat,file3.mat'}
n = numel(filenames) ;
A = zeros([],n) ;
for i = 1:nume1(filenames)
load(filenames{i})
THC1= THC_sim_cum ;
A(:,i) = THC1 ;
end
xlswrite('e.xlsx',A)
  댓글 수: 1
Prasad Joshi
Prasad Joshi 2022년 1월 18일
Bro thank you for code but the code generated 6 different excel files ....Instead can we but all the values in same excel files any input for that ...thank you btw bro I have attached code you suggested for your information BKv400_1_THC_sim_mass_cum has matrix cells of 70381*1

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

Community Treasure Hunt

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

Start Hunting!

Translated by