Keeping the Dimensions when converting a cell array to a matrix

조회 수: 7 (최근 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 CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by