App Designer, "Conversion to double from struct is not possible." when using importdata()

조회 수: 8 (최근 30일)
This error occurs at the following line in my script:
app.M(t) = importdata(filepathtrades{t,1});
For context, this is in the loop:
for t = 1:app.DirLeng
app.M(t) = importdata(filepathtrades{t,1});
end
Where app.DirLeng = 3 in this case. I am trying to import multiple excel files (3) specified by the filepaths contained in a 3 by 1 cell array (of strings). I am attempting to store these two dimensional data matrices into a three dimensional matrix, M. I have tried both:
app.M(t) = importdata(filepathtrades{t,1});
and
app.M(:,:,t) = importdata(filepathtrades{t,1});
but still get the same error. I have re-created this section of my code into a standard MATLAB script outside of App Designer and it performs just as I expect it to. Does anyone know if this is some sort of bug in App Designer? or if my syntax is wrong? or if there is an alternative way to do this that won't throw an error?
I can add more info about my script if need be. Thanks for helping!
  댓글 수: 1
Matthew Sisson
Matthew Sisson 2017년 6월 8일
편집: Matthew Sisson 2017년 6월 8일
I divided the line into two:
D = importdata(filepathtrades{t,1});
app.M(t) = D;
but I still get the same error, although this time it is at the second line. D is a 1x1 structure with 3 fields inside, each a 1x1 structure as well. I'm really trying to save a structure within another.

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

채택된 답변

Chris Portal
Chris Portal 2017년 6월 10일
I think the problem is you may have initialized app.M to [] somewhere. By doing so, and then indexing into it using app.M(t), MATLAB is interpreting M as a numeric array you're trying to index into. This causes MATLAB to try and convert your struct D into a double, which it can't. Instead, you can either:
  1. Not initialize M, and let it get initialized to a struct on its own when you first assign D to it.
  2. Or initialize M to an empty struct with the correct field names. Something like:
struct('data', [], 'textdata', [])
  댓글 수: 2
Stephen23
Stephen23 2017년 6월 10일
편집: Stephen23 2017년 6월 10일
Indeed. Once the the unrelated aspects of the code are stripped away (e.g. importdata, etc) we are left with assigning a structure to a double:
>> M = [1,2,3];
>> M(2) = struct('a',1)
??? The following error occurred converting from struct to double:
Error using ==> double
Conversion to double from struct is not possible.
>>
Solution: do not assign a structure to a double. And read the MATLAB error messages: they are not cryptic messages.
Matthew Sisson
Matthew Sisson 2017년 6월 12일
I think this will work, but I found a workaround that is just as effective. With App Designer, it is my understanding that you need to initialize your variables in the "properties" section. I had initialized M as:
M;
I tried to not specify the type but I think MATLAB assumes that it is a double by default? I could have initialized this as a structure as you both mentioned and I'm sure it will work perfectly. I wound up using the following:
app.M = importdata(filepath);
if app.CheckBox.Value
for t = 2:app.DirLeng
Mtemp = importdata(filepathtrades{t});
app.M = [app.M,Mtemp];
end
end
I'm not sure which way is more proper or faster, but I think this method happened to work better for my program since there may or may not be multiple sets of data to import, depending on the state of the checkbox.
Thank you both for your help and you quick responses! Much appreciated!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by