Keeping the Dimensions when converting a cell array to a matrix

12 views (last 30 days)
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.

Answers (1)

Ameer Hamza
Ameer Hamza on 25 Apr 2018
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.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!