Keeping the Dimensions when converting a cell array to a matrix
6 次查看(过去 30 天)
显示 更早的评论
It's my first year using MATLAB so forgive me for asking such questions
I am currently trying to use an inputdlg menu to create a function that will solve a static problem with different variables.
I'm having trouble converting the cell array to a matrix and keeping the same dimensions For example:
A = inputdlg(prompt,title,dims,definput);
B = cell2mat(A);
Where A is a 5x1 cell and B is a 5x2 char array I figured I would convert B to a number later using str2double but the 5x2 char array is making it difficult and since the function depends on the user input, the array will always vary.
Is there a solution to this I am not seeing? or yet a better method? Any sort of help is very much appreciated.
0 个评论
回答(1 个)
Ameer Hamza
2018-4-25
Although you can just use str2num() on a 5*2 char array. But there can be more flexible solutions, for example you can use cellfun() to apply a specific function to all elements of a cell matrix and return a solution. Also, in your case, using string class is better than char array. An example,
B = cellfun(@(x) string(x), A); % convert ot string array
B = cellfun(@(x) str2double(x), A); % directly convert to double.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!