App Designer, "Conversion to double from struct is not possible." when using importdata()
1 次查看(过去 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!
采纳的回答
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:
- Not initialize M, and let it get initialized to a struct on its own when you first assign D to it.
- Or initialize M to an empty struct with the correct field names. Something like:
struct('data', [], 'textdata', [])
2 个评论
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File 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!