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?
采纳的回答
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 个评论
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
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!