why I am getting Unbalanced or unexpected parenthesis or bracket?
Ältere Kommentare anzeigen
I am reading a variable from imu_data.bin by default it is in big endian to change it to little endian I am changing the byte position by following but it stating the unexpected assignment error. Pls help
filename = 'imu_data.bin';
file = fopen(filename, 'rb');
i = 0;
while ~feof(file)
x = fread(file,4,'uint8');
a[i+0] = x[i+3];
a[i+1] = x[i+2];
a[i+2] = x[i+1];
a[i+3] = x[i+0];
filename1 = 'imu_dat.bin';
file2 = fopen(filename1, 'w');
fwrite(file2,a,'uint8');
i=i+1;
fclose(file2);
end
% Close serial port and file
fclose(file);
Antworten (3)
Star Strider
am 27 Nov. 2015
0 Stimmen
I see two significant problems, one of which will cause that error:
- Unlike C, MATLAB indexing begins with 1, not zero, so before the loop, initialise i = 1; instead of i = 0;
- Again unlike C, MATLAB uses parentheses ‘()’ not square brackets ‘[]’ to denote subscripts, so ‘a[i+0] = x[i+3];’ will throw the ‘Unbalanced or unexpected parenthesis or bracket.’ error. Change that line (and others like it) to: ‘a(i+0) = x(i+3);’, and your code will not throw that error.
4 Kommentare
Nikhil Singh
am 28 Nov. 2015
Bearbeitet: Nikhil Singh
am 28 Nov. 2015
Image Analyst
am 28 Nov. 2015
Attach your new code. Your original code does not use any variable called "p". And why do you think p should have 4 elements, when it actually only has one element?
Nikhil Singh
am 28 Nov. 2015
Bearbeitet: Walter Roberson
am 28 Nov. 2015
Walter Roberson
am 28 Nov. 2015
Do not keep opening the output file in the loop.
If you must use your code then add one to each of the indices on the right hand side. For example,
g(1) = v(4);
g(2) = v(3);
g(3) = v(2);
g(4) = v(1);
But you do not need to handle all of those variables one by one: you are doing the same thing to every group of 4 bytes so just loop doing 4 bytes at a time like I show in my Answer.
Walter Roberson
am 28 Nov. 2015
feof() cannot predict End Of File. feof() is not true until you have tried to do a read and the read fails. (This is true in C as well.) The way you detect that the read fails is that the size of data returned from the fread() is 0.
But your indexing into a and i is broken anyhow. You read in 4 bytes on the second iteration, but you try to access the 5th byte of it because you incremented i. And opening and writing the file each time is a waste. I also do not recommend writing to the same file as you are reading from unless you know what you are doing.
filename_in = 'imu_data.bin';
filename_out = 'new_imu_data.bin';
file_in = fopen(filename_in, 'rb');
if file_in < 0; error('failed to open input file %s', filename_in); end
file_out = fopen(filename_out, 'wb');
if file_out < 0; fclose(file_in); error('failed to open output file %s', filename_out); end
while true
x = fread(file_in,4,'uint8');
if isempty(x); break; end %end of file
a = x([4 3 2 1]); %reorder the bytes
fwrite(file_out, a, 'uint8');
end
fclose(file_out);
fclose(file_in);
Now if you really want to,
movefile(filename_out, filename_in);
1 Kommentar
Nikhil Singh
am 28 Nov. 2015
Ganesh Gaud
am 18 Mär. 2018
Bearbeitet: Ganesh Gaud
am 18 Mär. 2018
0 Stimmen
Can some one explain why it is giving error.
>> for i= 1:n Knew =zeros(dof); k1 = [rc1(i);rc2(i);(rc1(i)+rc2(i))/L(i);(-(rc1(i)+rc2(i)))/L(i)]; k2 = [rc2(i);rc1(i);(rc1(i)+rc2(i))/L(i);(-(rc1(i)+rc2(i)))/L(i)]; k3 = [(rc1(i)+rc2(i))/L(i);(rc1(i)+rc2(i))/L(i);(2*(rc1(i)+rc2(i)))/(L(i)^2);(-2*(rc1(i)+rc2(i)))/(L(i)^2)]; k4=-k3; K=[k1 k2 k3 k4]; fprintf('Member Number ='); disp(K); for p=1:4 for q=1:4 Knew((1(i,p)),(1(i,q))) =K(p,q); Knew((1(i,p)),(1(i,q))) =K(p,q); ↑ Error: Unbalanced or unexpected parenthesis or bracket.
1 Kommentar
Walter Roberson
am 18 Mär. 2018
You cannot index the digit 1 with anything. Perhaps your 1(...) should be L(...)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!