Error using reshape - Please help!

Trying to create this out put variable but I'm getting an error saying "to RESHAPE the number of elements must not change". Please help!
datadir = cd; % define current path
filename = uigetfile('SSRT_results.xlsx');
[~, ~, raw] = xlsread(filename,'Sheet1','A2:G321');
stringVectors = string(raw(:,3));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,4,5,6,7]);
data = reshape([raw{:}],size(raw));

5 Comments

what does raw{:} return?
and size(raw)?
Maria Y
Maria Y on 22 Jan 2019
Edited: per isakson on 22 Jan 2019
Thank you for your quick response! Sorry ahead of time for being extremely new to Matlab.
raw{:} is just giving me NaN many times.
size(raw) is giving me 321 1
size(raw) is giving me 321 1
That does not make sense after reading in 'A2:G321' . You would expect 320 x 7.
Can you describe how you actually want to reshape the raw data?
because it just seems like youre trying to set data = raw.
raw is your 320x7 matrix, as described by walter.
but in your first argument in reshape, raw{:} is just raw turned into a column vector... and your second argument looks like youre just trying to reshape that column vector back into a 320x7.
I was able to figure out my issue (something wrong with the excel file), thank you so much for your help!!

Sign in to comment.

Answers (1)

The number of elements in the cell array raw is not necessarily the same as the number of elements contained in those cells. In this example the three element cell array raw contains nine numbers.
raw = {[1 2 3], [4 5 6], [7 8 9]}
combine = [raw{:}]
size(combine)
size(raw)
Imagine you had a dresser with three drawers, each of which contained two pieces of clothing. You have three drawers but six pieces of clothing in total.
What would you expect / intend this line of code to do for the raw cell array I created above? What is the exact result you want?
data = reshape([raw{:}],size(raw));

1 Comment

In the case that each element of the cell array contains a scalar numeric value (including nan), then [raw{:}] will have the same number of elements as raw has.
However, if even one of the elements contains a character vector with more than one character, or contains the empty character vector, then [raw{:}] will have a different number of elements than raw does.

Sign in to comment.

Asked:

on 22 Jan 2019

Commented:

on 22 Jan 2019

Community Treasure Hunt

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

Start Hunting!