Effacer les filtres
Effacer les filtres

Error reading structure from .mat file

4 vues (au cours des 30 derniers jours)
Max C
Max C le 14 Sep 2020
Commenté : Max C le 14 Sep 2020
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 commentaires
Walter Roberson
Walter Roberson le 14 Sep 2020
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 le 14 Sep 2020
I follow, so what I have done now is perhaps the best way to deal with it in this case. Thank you!

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by