Assign values to empty matrix

조회 수: 5 (최근 30일)
Mridul Garg
Mridul Garg 2016년 7월 17일
댓글: Mridul Garg 2016년 7월 17일
Hello
I want to create an empty matrix of unknown rows and known columns, then run a loop and assign values to it iteratively. So for example, my code should be something like-
result=zeros(10,3);
for i=1:10
num=1;file=i;
result(i,1)=strcat(num,'_',file);
result(i,2)=tp; % result of some calculation
result(i,3)=delay; % result of another calculation;
end;
Matrices in matlab cannot have both string and numeric types, so how do I proceed doing this?
Thanks in advance!

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 17일
편집: Azzi Abdelmalek 2016년 7월 17일
You can use cell arrays
A={1 2 'abc' 'efg' [1 2;3 4] [] ''}
Remarque: result=zeros(10,3); is not an empty matrix. With cell arrays you cen write:
result=cell(10,3)
for i=1:10
num=1;
file=i;
result{i,1}=sprintf('%d_%d',num,file);
tp=rand;
delay=rand;
result{i,2}=tp; % result of some calculation
result{i,3}=delay; % result of another calculation;
end;
result
  댓글 수: 2
Mridul Garg
Mridul Garg 2016년 7월 17일
I ran the above code with one modification- result=cell(10,3); It gives me the following error-
Conversion to cell from char is not possible.
Mridul Garg
Mridul Garg 2016년 7월 17일
Sorry, my bad. I had to change the parenthesis. The problem is fixed now.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by