필터 지우기
필터 지우기

How to write each iteration (data) in for loop to excel and how to read multiple excel files together?

조회 수: 8 (최근 30일)
Hello,
I have 20 excel files (subjects) and I want my code to read each excel file in one matlab code and perform the following operations for each excel file. In the below code, my code performs calculations for only one subject (excel file).
Each subject has 15 stages for the two variables and I want to write the value in each stage for the two variables (c and n) to the excel. However my code only writes the last value since it overwrites it. The aim of the code is to find raw values and finding the corresponding raw values of the two variables (c and n) for each 15 stage. And write them to the excel in this format (above are the names and below are their values):
c-1st stage c-2nd stage......c-15th stage n-1st stage n-2nd stage......n-15th stage
5 3 3 2 3 ..... 5
Could you please help me to solve these problems? I have tried some of the explanations in the forum but it did not work. Thank you so much.
data=xlsread('subject1');
count=data(:,1);
col=data(:,2);
resp1=data(:,3);
for j=0:14
m=1;
raw(m)=find((count ==j) & (resp1 ==1) ;
c(m)=col(raw(m));
n(m)=resp1(raw(m));
A=table(c(m), n(m));
writetable(A,'output1.xlsx');
m=m+1;
end

채택된 답변

Abderrahim. B
Abderrahim. B 2022년 8월 7일
Hi!
With the below command you are overwriting output1.xlsx file, hence you are only getting the last written file.
writetable(A,'output1.xlsx');
Try something like this:
filename = "output" + num2str(m) + ".xlsx" ;
writetable(A, filename);
Hope this helps
  댓글 수: 2
Go Sugar
Go Sugar 2022년 8월 7일
Thank you so much for the prompt answer. It seems correct. However, I could not figure out if it works because it gives an error for using +, says that matrix dimensions must agree. I will work on it and I will try solving this error.
Go Sugar
Go Sugar 2022년 8월 7일
I have solved this problem =), how am I supposed to write the data of each subject row by row in the same excel file?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by