0

Here is the code:

>> fid = fopen('dump.dat');
>> line_ex = fgetl(fid)

line_ex =

    '106,94,241,11,190,237,209,224'

>> newStr = split(line_ex,',')

newStr =

  8×1 cell array

    '106'
    '94'
    '241'
    '11'
    '190'
    '237'
    '209'
    '224'

>> cell2mat(newStr)
Error using cat
Dimensions of matrices being concatenated are not consistent.

Error in cell2mat (line 83)
            m{n} = cat(1,c{:,n});

This error is mind boggling. I am not trying to concatenate anything by running the cat function.

The '106' is actually seen by MATLAB is three separate ASCII characters. I just need to convert the cell array into a simple vector of numbers.

7
  • 2
    You want to convert the strings to numbers. You can do this with str2mat or str2double. Commented Jul 5, 2022 at 22:34
  • Why isn't there a str2int like str2double? It seems that I can use the str2double directly on the cell array. That is interesting, Commented Jul 5, 2022 at 23:15
  • 3
    Double is the core type in MATLAB, integers were tagged on later, and cannot be used in many places. You should use doubles for everything unless the reduced memory footprint of another type makes sense. Commented Jul 6, 2022 at 2:44
  • 1
    It’s better to use doubles for everything, including integers and indexing, unless you’re worried about memory consumption. Integer arithmetic in MATLAB is … awkward. Commented Jul 7, 2022 at 1:16
  • 2
    If the values you use are all integers smaller than 2^52, then arithmetic with them is exact. If you are unsure, use round. Commented Jul 7, 2022 at 13:30

1 Answer 1

1

cell2mat can only convert cell array to char type, so if the elements in cell have different length, error will occur. Temporally, there is no good way to handle your problem, but you can use for loop.

for i = 1:length(newStr)
    num(i,1) = str2num(cell2mat(newStr(i)));
end

Apologize for my poor English.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.