Andre A
Andre A

Reputation: 15

Matlab -- Conversion to double from cell is not possible

Can someone tell me why I am receiving this error -- ??? The following error occurred converting from cell to double: Error using ==> double Conversion to double from cell is not possible.

Error in ==> test at 18 CX(end+1,:) = temp(1);

Here is the code:

file = fopen('C:\Program Files (x86)\Notepad++\testFile.txt'); % open text file

tline = fgetl(file); % read line by line and remove new line characters

%declare empty arrays
CX = [];
CY = [];
CZ = [];

while ischar(tline) % true if tline is a character array
    temp = textscan(fid,'%*s%f%f%f','Delimiter',',<>'); % loads the values from all rows with the specified format into the variable data

    CX(end+1,:) = temp(1);
    CY(end+1,:) = temp(2);
    CZ(end+1,:) = temp(3);

    tline = fgetl(file);
end

fclose(file); % close the file

plot3(CX, CY, CZ) % plot the data and label the axises
xlabel('x')
ylabel('y')
zlabel('z') 
grid on
axis square

Upvotes: 1

Views: 11154

Answers (2)

tmpearce
tmpearce

Reputation: 12693

Use cell2mat to convert from a cell array (what textscan returns) to a numeric array, which you can use (like append to, in your case) with other numeric arrays.

I would also recommend using vertcat rather than the approach you've taken to concatenating:

CX = vertcat(CX, cell2mat(temp(1)));

Or, you could read all 3 values in to a row and concatentate into a N-by-3 matrix instead... lots of options.

Upvotes: 3

Dan
Dan

Reputation: 45752

Quick guess: does using curly braces help?

CX(end+1,:) = temp{1}

Upvotes: 3

Related Questions