Clear Filters
Clear Filters

Unable to perform assignment because the left and right sides have a different number of elements.

20 views (last 30 days)
Unable to perform assignment because the left and right sides have a different number of elements.
Error in Read_Data_File (line 15)
block(jj) = fscanf(fid, '%f', 1);
Below are two pieces of the overall code that I am working with, with the first part establishing the txt file that is read by the second part. I am receiving the above error message whenever I attempt to run the code.
clear; clc;
% Define the parameters
A = 5;
B = 6;
C = 7;
D = 3;
omega1 = 200 * 2 * pi;
omega2 = 400 * 2 * pi;
omega3 = 900 * 2 * pi;
% Define the sample frequency
fs = 10000; % Hz
% Define the time vector
t = 0:1/fs:1;
% Calculate the signal
x = A * cos(omega1 * t) + B * sin(omega2 * t) + C * cos(omega3 * t) + D;
% Save the signal to a file
filename = 'Simulated_Data_file.txt';
fid = fopen(filename, 'w');
for ii = 1:length(x)
fprintf(fid, '%f\n', x(ii));
end
fclose(fid);
clear; clc;
% Define the number of points per block
block_size = 1000;
% Open the file
filename = 'Simulated_Data_file.txt';
fid = fopen(filename, 'r');
% Read in the data
Data_File = cell(100, 1); % 100 blocks of 1000 points each
for ii = 1:100
block = zeros(block_size, 1);
for jj = 1:block_size
block(jj) = fscanf(fid, '%f', 1);
end
Data_File{ii} = block;
end
% Close the file
fclose(fid);

Answers (1)

Chris
Chris on 12 Feb 2023
Edited: Chris on 12 Feb 2023
You've hit the end of the file.
The file has 10001 data-containing lines.
Your code errors at ii==11, jj==2.
block_size (1000) x 11 + 2 = 10002.
The last line in the file is empty (which fscanf reads with a size of 0x0). Matlab is trying to add that to the jjth element in the block array, assuming a value with a size of 1x1. That's where the size mismatch occurs.
  3 Comments
Chris
Chris on 12 Feb 2023
Edited: Chris on 12 Feb 2023
@Brandon that depends on what you're trying to accomplish.
Are you trying to fill 100 blocks of 1000, stop in the middle of a block when you reach the end of the file, or have the number of data points be a multiple of 1000 and stop at the end of a block (and stop the loop when the file is finished)?
Chris
Chris on 12 Feb 2023
Edited: Chris on 12 Feb 2023
Here's a guess. In the first script, do:
t = 1/fs:1/fs:1; % Or t = 0:1/fs:(1-1/fs);
In the second:
filename = 'Simulated_Data_file.txt';
fid = fopen(filename,'r');
data = fscanf(fid,'%f'); %Ignores the empty line
fclose(fid);
block_size = 1000;
temp = reshape(data,block_size,[]); % A 1000-by-N double array
% The final cell array (but do you really need this, or can you use temp?)
Data_File = num2cell(temp, 1).';
In the first script (as with reading the file) it's more in the spirit of Matlab, and possibly more efficient, to write the file without the loop:
fid = fopen(filename, 'w');
fprintf(fid, '%f\n', x);
fclose(fid);
If performance isn't a huge concern, consider using higher-level functions, which can be easier to work with:
writematrix(x.', filename);
data = readmatrix(filename);

Sign in to comment.

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!