CptGoodar
CptGoodar

Reputation: 303

Convert values from a single cell into a matrix

I am trying to create a small script that allows the user the ability to create a new matrix (10x8) from scratch, that will later be used in bi-linear interpolation.

Currently my method for doing this is using the prompt function with 10 rows within the pop-up block. The user would then input the 8 values for each row that they would like. This would output a 10x1 cell with the values within. Then this 10x1 cell needs to convert to a 10x8 matrix

Below is a simplified example (5x3) matrix idea with my current code.

User enters;

User Data Input

Once the data has been entered I've tried to get Matlab to convert those numbers into, in this example, a 5x3 matrix, such as

1 2 3

4 5 6

7 8 9

1 5 9

7 5 3

using this code

prompt = {'Enter the first row values','Enter the second row values','Enter the third row values'...
           'Enter the forth row values', 'Enter the fifth row values'}%, 'Entre the sixth row values'...
         %'Enter the seventh row values', 'Enter the eighth row values', 'Enter the nineth row values' ...
         %'Enter the tenth row values'};
     title = ' bi-linear interpolation grid ';
     dims = [1 50]; 

answer = inputdlg(prompt,title,dims); 

bi_linear_interpolation_grid = cell2mat(answer);

However this creates a 5x5? Char with the values in. Image below

Matlab Result

I have also tried to usestr2double as well, another method that has been previously used online, that didn't work at all.

I understand that this may not be the most efficient method for entering data (I am fairly new to Matlab), but I found that this way was a quick and clean method. If you know a simpler way please point me in the right direction

Thank you very much for any help you are able to provide.

Upvotes: 0

Views: 59

Answers (1)

Zep
Zep

Reputation: 1576

You are looking for the split() function:

answer = {'1 2 3' ; '4 5 6' ; '7 8 9' ; '10 11 12' ; '13 14 15'};
answer = str2double(split(answer))

answer =

 1     2     3
 4     5     6
 7     8     9
10    11    12

As for the simpler way to input the data, I like to copy and paste directly from Excel to the Variables panel in the workspace. Remember that to paste from Excel to Matlab you should use CTRL + SHIFT + V instead of standard CTRL + V.

Upvotes: 2

Related Questions