Error Using a loop: Conversion to double from cell is not possible.

Hello everyone,
On executing following code:
for w = 1:10
PN = PName{w};
CS = CSize{w};
Project = [PN CS];
Project = sortrows(Project,1);
Project = Project(11:end,1:end);
Proj(w) = Project;
end
I am getting error: Conversion to double from cell is not possible..
Code is running well till Project = Project(11:end,1:end);
Size of Project is: 4227x2 cell.
Error is while assigning a values to a new variable using a loop at Proj(w) = Project;
Any help would be appreciated :-)
Regard,s
Waqar Ali

댓글 수: 3

Well, as you state Prorject is a cell array. From the error message, we can deduce that Proj is a matrix of doubles. So, of course you can't store a whole cell array as an element of a matrix.
Since you haven't said what you're trying to do, we can't tell you how to fix it, other than not trying to store cell arrays in matrices.
Hello Guillaume,
Since, loop range is from 1 to 10 and once the required data is executed (till Project = Project(11:end,1:end); ). I want to create a new variable and save this output in that. It means if w = 1 is executed: I should get new variable with name Proj1 and should have the output of Project; Once it is saved. It should be done for 2,3,...10. I hope, you understand :-)
Thank you for your time.
"I want to create a new variable ... I should get new variable with name Proj1..."
Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
In contrast indexing is simple, neat, easy to debug, and very efficient. You should use indexing, just like madhan ravi showed in their answer.

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

 채택된 답변

Peter Jarosi
Peter Jarosi 2019년 7월 10일
편집: Peter Jarosi 2019년 7월 10일
Perhaps what you need is struct. Try the following:
s = struct();
for w = 1:10
PN = PName{w};
CS = CSize{w};
Project = [PN CS];
Project = sortrows(Project,1);
Project = Project(11:end,1:end);
FieldName = strcat('Proj', num2str(w));
s.(FieldName) = cell2mat(Project);
end
You will get your 10 "variables" within structure s. You can refer them as s.Proj1, s.Proj2, etc.
Please let me know if it works for you.

댓글 수: 7

In my opinion, numbered field names are hardly better than numbered variables. You're still embedding an index into a name, so end up manipulating (slow) strings instead of (fast) numbers.
You are right! But Waqar Ali Memon said "I should get new variable with name Proj1".
I agree with you it would be much better using cell like this:
Proj = cell(10,1);
for w = 1:10
PN = PName{w};
CS = CSize{w};
Project = [PN CS];
Project = sortrows(Project,1);
Project = Project(11:end,1:end);
Proj{w} = cell2mat(Project);
end
Well Peter that’s what was shown in my answer
"Waqar Ali Memon said "I should get new variable with name Proj1"
Yes, for some reason beginners often want to do that, not realising that they're making their life harder instead of easier. Sometimes, the best answer is: you don't actually want to do that.
madhan ravi : Exactly, in terms of using cells instead of structure. I think Waqar Ali Memon also needs to convert the type of Project from cell to array. Notice func cell2mat() in
Proj{w} = cell2mat(Project);
madhan ravi
madhan ravi 2019년 7월 10일
편집: madhan ravi 2019년 7월 10일
Well it was mentioned nowhere whatsoever by the OP. https://in.mathworks.com/matlabcentral/answers/471071-error-using-a-loop-conversion-to-double-from-cell-is-not-possible#comment_723218 - in case if you had missed Guillaume’s comment
Peter Jarosi
Peter Jarosi 2019년 7월 10일
편집: Peter Jarosi 2019년 7월 10일
madhan ravi : I read Guillaume 's comments carefully (again). I didn't find what I missed but I found the best part of it: "Sometimes, the best answer is: you don't actually want to do that." I really like it. :)

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

추가 답변 (2개)

madhan ravi
madhan ravi 2019년 7월 10일
Proj = cell(10,1); % outside loop
Proj{w} = ... % inside loop , leave the rest unchanged

댓글 수: 3

Thank you for reply.
By this I am getting output like:
2019-07-10_15h11_051.png
But I require output like:
Proj1 having size 4227x2 cell
Proj2 having size of 4430x2 cell
and so on. It means i need 10 variables having the dimensions as shown in the attached Image.
+1 simple and efficient MATLAB solution.

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

Waqar Ali Memon
Waqar Ali Memon 2019년 7월 10일

1 개 추천

Thank you so much madhan ravi, Guillaume and Stephen Cobeldick. I wanted a loop and the code from Peter Jarosi did same. Though, it won't be proper solution but it helped me :-)
Thank you everyone :-)
Regards,
Waqar Ali Memon

댓글 수: 1

Waqar Ali Memon : You're very welcome!
I totally agree with programmer gurus madhan ravi, Guillaume , and Stephen Cobeldick. It is not recommended using dinamically changed variable names, and string operations instead of numbers, especially when we develope big applications. I appreciate their comments.
But sometimes we have to break rules, we need ugly tricks in order to solve our small problems quickly. :-)
Good luck!

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2017b

질문:

2019년 7월 10일

댓글:

2019년 7월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by