why I am getting Unbalanced or unexpected parenthesis or bracket?

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
Star Strider am 27 Nov. 2015

0 Stimmen

I see two significant problems, one of which will cause that error:
  1. Unlike C, MATLAB indexing begins with 1, not zero, so before the loop, initialise i = 1; instead of i = 0;
  2. 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

I tried with ur method by assigning initial element as 1 then it shows
a(1) = p(4);
a(2) = p(3);
a(3) = p(2);
a(4) = p(1);
Attempted to access p(4); index out of bounds because numel(p)=0.
Error in writing_LE (line 10) a(1) = p(4);
and another way which I tried is a(1) = p(3); a(2) = p(2); a(3) = p(1); a(4) = p(0); then the error is Attempted to access p(0); index must be a positive integer or logical.
Error in writing_LE (line 13) a(4) = p(0);
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?
this is my actual code
filename = 'imu_data.bin';
file = fopen(filename, 'rb');
while ~feof(file)
filename1 = 'imu_dat.bin';
file2 = fopen(filename1, 'w');
p = fread(file,4,'uint8');
a(1) = p(3);
a(2) = p(2);
a(3) = p(1);
a(4) = p(0);
fwrite(file2,a,'uint8');
q = fread(file,4,'uint8');
b(1) = q(3);
b(2) = q(2);
b(3) = q(1);
b(4) = q(0);
fwrite(file2,b,'uint8');
r = fread(file,4,'uint8');
c(1) = r(3);
c(2) = r(2);
c(3) = r(1);
c(4) = r(0);
fwrite(file2,c,'uint8');
s = fread(file,4,'uint8');
d(1) = s(3);
d(2) = s(2);
d(3) = s(1);
d(4) = s(0);
fwrite(file2,d,'uint8');
t = fread(file,4,'uint8');
e(1) = t(3);
e(2) = t(2);
e(3) = t(1);
e(4) = t(0);
fwrite(file2,e,'uint8');
u = fread(file,4,'uint8');
f(1) = u(3);
f(2) = u(2);
f(3) = u(1);
f(4) = u(0);
fwrite(file2,f,'uint8');
v = fread(file,4,'uint8');
g(1) = v(3);
g(2) = v(2);
g(3) = v(1);
g(4) = v(0);
fwrite(file2,g,'uint8');
w = fread(file,4,'uint8');
h(1) = w(3);
h(2) = w(2);
h(3) = w(1);
h(4) = w(0);
fwrite(file2,h,'uint8');
x = fread(file,2,'uint8');
fwrite(file2,x,'uint8');
fclose(file2);
end
% Close serial port and file
fclose(file);
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.

Melden Sie sich an, um zu kommentieren.

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

by this code clc; clear all; close all; filename_in= 'imu_data.bin'; file_in = fopen(filename_in, 'rb'); if file_in < 0; error('failed to open input file %s', filename_in); end filename_out = 'imu_NEW.bin'; 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);
error msg:- Index exceeds matrix dimensions.
Error in write_in_le (line 15) a = x([4 3 2 1]); %reorder the bytes

Melden Sie sich an, um zu kommentieren.

Ganesh Gaud
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

You cannot index the digit 1 with anything. Perhaps your 1(...) should be L(...)

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 27 Nov. 2015

Kommentiert:

am 18 Mär. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by