Error using reshape with file input

For some reason reshape won't work and sadly I'm not sure why. Could someone please help me resolve this error? I think it might have something to do with my a_matrix.txt file yet messing with that gives no results. If anyone was wondering, the reason I am writing this code is for it to read a linear system off the file and then solve it by writing it onto another file. All advice is helpful.
% solve a linear system Ax = b by reading A and b from input file
% and then writing x on output file.
clear;
clc;
input_filename = 'my_input.txt';
output_filename = 'my_output.txt';
% read data from file
f = textscan('a_matrix.txt', '%f');
vals = reshape(f, 11, []).';
A = vals(:,1:10);
b = vals(:,11);
% solve the linear system
x = A\b;
% write output data on file
dlmwrite('my_output.txt',x,'delimiter',',','precision',4);
% print screen
fprintf('Solution vector is: \n');
fprintf('%4.2f \n', x);
my input

6 Comments

If you have a problem with reshape function, show us your data, and the problem you've got
What is the error? At a guess I would assume it is telling you that the dimensions do not match up - i.e. are not a multiple of 11.
There's not much for us to say on that though - either the data you read has a number of elements exactly divisible by 11 or it doesn't. If it doesn't then you will get an error and how to fix that is entirely dependent on why it is happening and whether your expectation of it being divisible by 11 is wrong or whether you have elements in 'f' that are not part of your actual data.
Image Analyst
Image Analyst on 6 May 2016
Edited: Image Analyst on 6 May 2016
First read this link and then attach 'my_input.txt' (maybe not since you don't seem to use it), and 'a_matrix.txt'.
Yes, the error is "Error using reshape Product of known dimensions, 11, not divisible into total number of elements, 1."
How do you want to reshape your matrix?
MATLAB reads data in by columns, so I used 'reshape' in order to fit the data within the array. Is there a better way to do it? I thought this would be simpler

Sign in to comment.

Answers (1)

dpb
dpb on 6 May 2016
Edited: dpb on 6 May 2016
With the other comments that we're "flying blind" not being able to see the input file structure, the first issue is that
f = textscan('a_matrix.txt', '%f');
is trying to parse the string 'a_matrix.txt' instead of a file since you haven't opened the file and obtained a valid file handle via fopen. After that, the result of the textscan will be a cell array, not a double array.
fid = fopen('a_matrix.txt');
vals = cell2mat(textscan(fid,'','collectoutput',1));
Now, whether you even need reshape at all or not will depend on how you organized the data file; the use of the empty format string has the salubrious effect that textscan will automagically return the shape of the file in the array.
ADDENDUM
OK, on the assumption it is the form with the trig functions you're trying to read, while rarely would I recommend it, here's a place where you could make use of eval. I made up a little sample input file as follows:
>> type rossi.dat
cos(45) 0 0
sin(45) 0 0
>> c=textread('rossi.dat','%s');
>> A=reshape([cellfun(@(x) eval(char(x)),c)],3,[]).'
A =
0.5253 0 0
0.8509 0 0
>>
NB: reshape is on number of columns since storage order is column major
Also, the standard trig functions in Matlab are in radians; you'll want to fixup the data file to use the 'd' form for input in degrees... cosd(), etc., ...

5 Comments

I got an output, but sadly it was of all 0s with a rank of 0 as well. Any suggestions on how to fix that? I tried messing with my input file yet it didn't give any results.
Well, w/o seeing the file, it's pretty tough to know where it went wrong. Is there a header line, for instance, maybe?
Nope, the file is the picture I attached in my post yet just the numbers of that. Same column arrangements and just the numbers without any equal signs. I tried splitting the numbers up, adding certain things (it gives me a rank = 1) yet nothing has worked.
It would've been much better to have attached the text of the file but if it contains the strings for the trig functions instead of the numerics, there's no magic when reading a file that the compute engine will be called and translate those expressions into numeric values. You need to either
a) insert the values in the file in those locations, or
b) you'll have to parse the input file on a line-by-line basis and convert to numeric value.
Oh, there are 12 elements in row 8, not 11...that'll cause heartache 'cuz then the array won't be divisible by 11 any longer. Check the rest again, too...

Sign in to comment.

Asked:

on 6 May 2016

Commented:

dpb
on 6 May 2016

Community Treasure Hunt

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

Start Hunting!