assign a string matrix in each row and column

9 Ansichten (letzte 30 Tage)
Zhonghao Liao
Zhonghao Liao am 29 Sep. 2016
Kommentiert: Guillaume am 30 Sep. 2016
I am trying to assign 100 times of sample data pattern to a string matrix by reading a txt file. Each time of data pattern fill in one row and each bit of pattern fill in one column. My program is as following:
function read_datapattern(fileName,startByte,byteLength)
fid = fopen(fileName,'rt');
if fid < 0
error('error opening file %s\n\n',fileName);
end
A = [];
for k = 1:100 %read times
%Read file as a set of strings, one string per line
for m = 1:startByte
nextline = fgets(fid);
end
A[k:] = fgets(fid);
for m = 1:byteLength - 1
nextline = fgets(fid);
A[k:] = strcat(A[i:], nextline);
end
for m = (startByte + byteLength):32767
nextline = fgets(fid);
end
a = length(A[k:]);
%fprintf(pattern);
fprintf('%d\n',a);
fprintf(A[]);
fprintf('\n');
%fprintf(pattern(262113:262144));
fprintf('\n');
num = str2num(pattern);
fprintf('%d\n',num);
nextline = fgets(fid);
nextline = fgets(fid);
end
fclose(fid);
end
The system editor displays the invalid syntax in assignment lines such that:
A[k:] = fgets(fid);
a = length(A[k:]);
fprintf(A[]);
I am a rookie of Matlab so I want to know how to accomplish this function. Thank you~

Antworten (1)

Stephen23
Stephen23 am 29 Sep. 2016
Bearbeitet: Stephen23 am 29 Sep. 2016
A[k:] = fgets(fid);
is not MATLAB syntax. It looks like Python syntax, and does not work with MATLAB.
Instead of guessing how MATLAB works, or using syntaxes from other languages, you should actually learn how to write MATLAB code. It is easy to learn how to write MATLAB code, because there are (free!) introductory tutorials:
If you had done these then you would know that MATLAB indexing is done using parentheses, not square brackets like you used. MATLAB also has a great documentation, so if you paid attention and looked up some of those warning messages then you would know how to do indexing properly:
Also note that your general algorithm is not very efficient. You would probably be much better off using textscan, importdata, or one of the other standard text-file import functions:
Extra it is common that beginners write a large amount of code (because they believe that they are being productive) without checking it and then once they have "finished" they find that it will not run, and have no idea why. Don't do this. Check and test every line of code as you write it. Do not guess: read the documentation for the functions that you use, for indexing, etc. Fix any warnings that the editor shows. Check that the line does exactly what you need it to do. Only start writing the next line when you are satisfied that the line is correct, and you understand what it is doing. Then you will not get into the mess that you are in now, with lots of code and no idea what any of it does.
  3 Kommentare
Stephen23
Stephen23 am 30 Sep. 2016
Bearbeitet: Stephen23 am 30 Sep. 2016
This occurs because you are using the : colon operator to allocate strings to all columns, but when the string has a different number of characters than there are columns then this is an error. This is easy to demonstrate:
>> A = [];
>> A(1,:) = 'abc' % length three
A =
97 98 99
>> A(2,:) = 'def' % length three
A =
97 98 99
100 101 102
>> A(3,:) = 'ghijk' % length five
??? Subscripted assignment dimension mismatch.
The easiest solution is either to use a cell array instead of a numeric/char array, or to stop wasting you time writing buggy code and use one of the inbuilt file reading functions, which I gave links to in my answer.
Your solution of expanding the array on each iteration is not very efficient MATLAB code anyway, as the last link in my answer explains.
Guillaume
Guillaume am 30 Sep. 2016
Note: since the release of R2016b you have to be careful what you call 'string'.
In the above comment, the strings are actually char arrays, in which each character is a single element of a char matrix. Since matrices can't have different number of elements per row, each row of the char array must have the same number of characters.
Since R2016b, there is now an official string class. Individual strings (of any length) can be stored in an array of strings:
A = string({'abc', 'def', 'ghijk'}); %create a 1x3 string array
A(2, 3) = 'aaaaaaa'; %expands it to a 2x3 string array with implicit conversion of char array to string

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by