Keeping the Dimensions when converting a cell array to a matrix

6 visualizaciones (últimos 30 días)
Diego Quiroz
Diego Quiroz el 8 de Abr. de 2018
Respondida: Ameer Hamza el 25 de Abr. de 2018
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.

Respuestas (1)

Ameer Hamza
Ameer Hamza el 25 de Abr. de 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.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by