Keeping the Dimensions when converting a cell array to a matrix

6 ビュー (過去 30 日間)
Diego Quiroz
Diego Quiroz 2018 年 4 月 8 日
回答済み: Ameer Hamza 2018 年 4 月 25 日
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.

回答 (1 件)

Ameer Hamza
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.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by