Dot indexing is not supported for variables of this type. - trying to create a function to upload files matrices

3 Ansichten (letzte 30 Tage)
I am trying to write a function that allows me to upload a chosen mat file and to take specific matrices from these files, so that these matrices can be used in another code.
I have written the following, to plot the data before writing is as a function, knowing that all signal matrices are named after the file as file.pulse0 and the time matrices are named as file.t:
name ='Woven_6';
load([name '.mat']); %I am wanting to load the 'Woven_6.mat' file
signal = name.pulse0(:,:); %I am wanting to load the 'Woven_6.pulse0' matrix
time = name.t(:,:); %I am wanting to load the 'Woven_6.t' matrix
plot(time, signal)
I intent to used this in another bit of code so that I can upload this time and signal values to calculate other variables, but I keep on getting this error:
"Dot indexing is not supported for variables of this type." - and it is stated that this error occurs on the lines 3 and 4 of the code.
As a function this would be written as:
function(signal, time, signal_reference, time_reference) = read_transmission[name]
name ='Woven_6_stepscan';
load([name '.mat']);
signal = name.pulse0(:,:);
time = name.t(:,:);
end
How can I fix the first bit of code so I can rewrite the function so that the latter works?
PS: I have seen the following link but I don't understand the answers to this question.

Akzeptierte Antwort

Stephen23
Stephen23 am 5 Mai 2022
Bearbeitet: Stephen23 am 6 Mai 2022
I am guessing that you have very badly-designed data, where the variable in the MAT file uses the same name as the file itself.
Assuming that there is exactly one variable in the MAT file, then you can obtain it like this:
F ='Woven_6.mat';
S = load(F); % Always LOAD into an output variable!
C = struct2cell(S);
assert(isscalar(C),'too many variables!')
signal = C{1}.pulse0;
time = C{1}.t;
plot(time, signal)
Better data design would not include meta-data in the variable name.
  2 Kommentare
Goncalo Costa
Goncalo Costa am 6 Mai 2022
Bearbeitet: Goncalo Costa am 6 Mai 2022
Thank you so much for your help, this worked perfectly. So, what made it not work was the fact that they were both named the same? Or just the way that the data file is?
Stephen23
Stephen23 am 6 Mai 2022
"So, what made it not work was the fact that they were both named the same?"
Correct: naming the variable dynamically makes it harder or more fragile to process.
"Or just the way that the data file is?"
The data file does not have to be like that: someone designed that data to be like that.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 5 Mai 2022
Bearbeitet: Walter Roberson am 5 Mai 2022
function [signal, time, signal_reference, time_reference] = read_transmission(name)
data = load([name '.mat']);
signal = data.pulse0;
time = data.t;
signal_reference = SOMETHING;
time_reference = SOMETHING;
end
  1 Kommentar
Goncalo Costa
Goncalo Costa am 5 Mai 2022
My data file would therefore be something like: Woven_6.mat ..... but this cannot be used for my signal as my signal is merely Woven_6.pulse0 and not Woven_6.mat.pulse0 .
Because of this I get the message: Reference to non-existent field 'pulse0'.
This is why last time I separated the data from the name. Knowing this, do you know how I could make this function open my files the way I described above?
Thank you so much for your help so far.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by