Using variables from .mat structures.

there are given 2 channel matrices, namely,
channel_1.mat and channel_2.mat which contain the struct data.
I need to use variables from this struct in folowing function:
But I don't know how I can implement this
%Import channel information
data=load('C:\Users\askrrow\Downloads\MSCSP_MoCo_2023SoSe_HW5\channel_1.mat')
data=load('C:\Users\askrrow\Downloads\MSCSP_MoCo_2023SoSe_HW5\channel_2.mat')
%Full correlation matrices estimation
Nt=length(data.nF);
for i=nF:Nt
f(Nt,nT,nF(i))
end
function Rh = f(Nt,nT,nF)
Rh=1/Nt.*sum(reshape(data.H(:,:,nT,nF),[],1).*reshape(data.H'(:,:,nT,nF),[],1),'all')
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
end

4 comentarios

Stephen23
Stephen23 el 1 de Jun. de 2023
Editada: Stephen23 el 1 de Jun. de 2023
When you do this:
data=load('C:\Users\askrrow\Downloads\MSCSP_MoCo_2023SoSe_HW5\channel_1.mat')
data=load('C:\Users\askrrow\Downloads\MSCSP_MoCo_2023SoSe_HW5\channel_2.mat')
%^^^ same variable names
then you overwrite the first DATA with the second DATA on the following line... I doubt that is very useful.
Probably you want two different variables, so that you can refer to both of them later:
P = 'C:\Users\askrrow\Downloads\MSCSP_MoCo_2023SoSe_HW5';
S1 = load(fullfile(P,'channel_1.mat'))
S2 = load(fullfile(P,'channel_2.mat'))
The cause of the error is clear:
Rh=1/Nt.*sum(reshape(data.H(:,:,nT,nF),[],1).*reshape(data.H'(:,:,nT,nF),[],1),'all')
% ^^ missing/extra operator
The syntax H'(..) is not valid. It seems that you meant H(..).
f(Nt,nT,nF(i))
I have two comments on this line.
First, since you're calling f with no output arguments the value you computed in the function is returned as the variable named ans and overwritten each iteration through your loop. You probably want to call f with an output argument and store that output argument in the elements / rows / columns / pages / etc. of an array, so you'll have access to the results from each iteration after the loop.
Second, having variables whose names differ only by case is likely to cause problems that may be very difficult to debug if the code gets much longer. Since these names are short and have some sort of physical meaning (based on the snippet from the paper or textbook that you gave) you might be insulated a bit from that potential confusion, but how likely is it you'll type nT when you actually mean Nt or vice versa? The human brain is pretty good at skipping small errors like taht because it "knows what you meant."
Did you catch the error I left deliberately in that previous sentence when you read it?
Ruslan Askarov
Ruslan Askarov el 2 de Jun. de 2023
with H' I meant hermitian transpose, maybe there is another operator.
Regarding to input data nT and nF are time and frequency variables respectively(Nt and Nf are total number of these variables). We can get these values from strcuct which we export from .mat file. The problem is that I don't know how to extract concrete data from sctructure. I need example.
There is also hint: data.H(:,:,nT,nF) but i'm not familiar with such syntax
Do you have some ideas??
Stephen23
Stephen23 el 5 de Jun. de 2023
Editada: Stephen23 el 5 de Jun. de 2023
"with H' I meant hermitian transpose, maybe there is another operator."
That is the correct operator, it is just the location is wrong. Perhaps you intended this:
data.H(:,:,nT,nF)'
% ^

Iniciar sesión para comentar.

Respuestas (1)

Sanjana
Sanjana el 5 de Jun. de 2023

0 votos

Hi,
The error message you encountered is due to attempting to access the transpose of the matrix H through parentheses directly. In MATLAB, the parentheses “()” operator has higher precedence than the transpose operator “'” according to the Operator Precedence Rules. Therefore, the expression “data.H'(:,:,nT,nF)” is interpreted as applying the parentheses to the transpose operator itself, rather than to the transposed matrix “ data.H'” resulting in an error.
To resolve this issue, it is recommended to assign the transposed matrix to a new variable before accessing its columns. By doing so, you explicitly indicate the desired order of operations.
Please, refer to the following link to know more about operator precedence rules,
For further clarification on accessing variables stored in a .mat file and accessing variables stored in a structure array, you can refer to the following links:
Hope this helps!

Categorías

Productos

Versión

R2020b

Preguntada:

el 1 de Jun. de 2023

Editada:

el 5 de Jun. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by