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

1 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2022-5-5
编辑:Stephen23 2022-5-6
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 个评论
Goncalo Costa
Goncalo Costa 2022-5-6
编辑:Goncalo Costa 2022-5-6
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 2022-5-6
"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.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-5-5
编辑:Walter Roberson 2022-5-5
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 个评论
Goncalo Costa
Goncalo Costa 2022-5-5
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by