Error using reshape with file input
Mostra commenti meno recenti
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 Commenti
Azzi Abdelmalek
il 6 Mag 2016
Modificato: Azzi Abdelmalek
il 6 Mag 2016
If you have a problem with reshape function, show us your data, and the problem you've got
Adam
il 6 Mag 2016
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
il 6 Mag 2016
Modificato: Image Analyst
il 6 Mag 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'.
David Rossi
il 6 Mag 2016
Azzi Abdelmalek
il 6 Mag 2016
How do you want to reshape your matrix?
David Rossi
il 6 Mag 2016
Risposte (1)
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 Commenti
David Rossi
il 6 Mag 2016
dpb
il 6 Mag 2016
Well, w/o seeing the file, it's pretty tough to know where it went wrong. Is there a header line, for instance, maybe?
David Rossi
il 6 Mag 2016
dpb
il 6 Mag 2016
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.
dpb
il 6 Mag 2016
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...
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
