Error reading structure from .mat file

I use Matlab to control a NI USB 6363 card. This card controls a measurement and reads some data. To improve my code, I write all parameters into a structure and save it as a . mat file. Later, I feed the structure only to the functions I use, before I used many variables from the workspace. The structure "par" contains all my parameters like "par.f_min" is the minimum frequency. I have a function that generates a signal and here I get the error because I switched to the structure as input of variables:
Error using frequenzy_vec_gen (line 78)
Cannot read file C:\Users\...\Par.mat.
In the main function I'm setting the parameters:
%...%
par = matfile('Par.mat', 'Writable', true);
load('Variables.mat');
par.f_min = 9; % Minimum frequency in Hz is 3. OT of f_fun
par.f_max = 10000; % Maximum frequency in Hz
par.srate = 150000; % Samplingrate ~ 10*f_max
par.n_f = 50; % Number of freqencies target
par.EC_Length = 4*(3/par.f_min); %Target length of EC in s
par.EC_I = 0.1; % "Current" for EC
par.R = 110.2; % Resistance of the serial Resistor for current determination
par.amp = 0.1;% Amplitude in V (100 mV). Can be later changed to a vector.
par.ww = round(9.832*sqrt(par.f_min)+5.332,0)-1;% Number of noises
%...%
Variables.mat contains some of the last measurements parameters from par. At some point I compare them to skip some time consuming functions like the signal generation:
function noise = frequenzy_vec_gen(par)
%...% Here par.f_vec is generated and checked if it fits to some conditions.
noise = zeros(par.n_p,par.ww); % Generating noise vector of signal length
phase_vec = zeros(length(par.f_vec),par.ww);
for w = 1:par.ww
% random phase vector
phase_vec(:,w) = 2*pi.*rand(length(par.f_vec),1).';
% Generate Sinewaves and add them together
for i = 1:length(par.f_vec)
noise(:,w) = noise(:,w) + par.amp.*sin(2 .* pi .* par.f_vec(1,i) .* par.t_vec + phase_vec(i,w)); % line 78
end
end
p2rms = peak2rms(noise);
[minn,nmin] = min(p2rms);
noise = noise(:,nmin);
save('noise.mat', 'noise');
par.f_vec = par.f_vec.';
end
I indicated the line with the error by a comment. For sure noise is not a noise, it's a signal. The loop generates ww multi sine signals with random phases and later the one with the lowest peak2rms is choosen as the test signal. When I try to debug and I break before the loop and then continou, I don't get an error.

4 件のコメント

Walter Roberson
Walter Roberson 2020 年 9 月 14 日
Hmmm... if you were to put a brief pause() in the for i loop, I wonder if the problem would still continue?
Does the problem always occur at the same w + i combination?
Max C
Max C 2020 年 9 月 14 日
編集済み: Max C 2020 年 9 月 14 日
I tried it last time with pause(1) and I get a different error:
Error using frequenzy_vec_gen (line 78)
Unable to read MAT-file C:\Users\maxim\OneDrive\Documents\MATLAB\DEIS - Project\DEIS - Ni 6363\Par.mat. Not a binary
MAT-file. Try load -ASCII to read as text.
Now I tried it with 3s and it worked... But why? So it is okay, to have this 3s pause, becaus I do not use the function regulary in the final program, but is there not a better solution?
Maybe I don't understand how the structure is working but in the main function I generate it with:
par = matfile('Par.mat', 'Writable',true);
So the structure is written to a .mat file. But it is still a strucutre in my workspace isn't it? If I now give this as an input to my next function (frequenzy_vec_gen) why do I get this delay?
Instead of using the pause() I tried now this:
noise = zeros(par.n_p,par.ww); % Generating noise vector of signal length
phase_vec = zeros(length(par.f_vec),par.ww);
f_vec = par.f_vec;
amp = par.amp;
t_vec = par.t_vec;
for w = 1:par.ww
% random phase vector
phase_vec(:,w) = 2*pi.*rand(length(f_vec),1).';
% Generate Sinewaves and add them together
for i = 1:length(f_vec)
noise(:,w) = noise(:,w) + amp.*sin(2 .* pi .* f_vec(1,i) .* t_vec + phase_vec(i,w));
end
end
And it works without the pause. This looks like, if I use "par." the variable is always read from the file and this needs some time (but matlab should still wait for it without creating an error?), if I define a variable before, matlab only reads the parameter once from the file. BUT: I assumed the structure "par" contains all the variables also in the workspace. Now, I see that it always checks first if this is also in the file. Maybe there is the possibility to switch this of? I could delete the "source" property from the file and later add it again?
I use it in this way because I have also one function that needs all the parameters but I can not give them as an input to the function, so I use this walkaround over the matfile (See my other open question).
Walter Roberson
Walter Roberson 2020 年 9 月 14 日
MATLAB needs to assume that another process might also be writing to the .mat file, so it has to refer back to the file for most operations.
Max C
Max C 2020 年 9 月 14 日
I follow, so what I have done now is perhaps the best way to deal with it in this case. Thank you!

サインインしてコメントする。

回答 (0 件)

カテゴリ

タグ

質問済み:

2020 年 9 月 14 日

コメント済み:

2020 年 9 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by