how to save variable data that we changed ?

this is my code:
nSamp = 3251;
Fs = 1024e3;
SNR = 40;
rng default
t = (0:nSamp-1)'/Fs;
x = sin(2*pi*t*100.123e3);
x = x + randn(size(x))*std(x)/db2mag(SNR);
[Pxx,f] = periodogram(x,kaiser(nSamp,38),[],Fs);
meanfreq(Pxx,f);
i change the variable in this code to my dataset . then after run, the variable go to its original data . how to solve this problem?

9 comentarios

KSSV
KSSV el 9 de Mzo. de 2021
Which variable you have changed and how you have changed?
NUR BATRISYIA HANNANI ZAIN AZMI
NUR BATRISYIA HANNANI ZAIN AZMI el 9 de Mzo. de 2021
Editada: NUR BATRISYIA HANNANI ZAIN AZMI el 9 de Mzo. de 2021
variable for x . i insert my own value of data for x. but then after run with new data for variable , the variable data go back to its original value.
where do you insert the value? your line
x = sin(2*pi*t*100.123e3);
overwrites all of x
NUR BATRISYIA HANNANI ZAIN AZMI
NUR BATRISYIA HANNANI ZAIN AZMI el 10 de Mzo. de 2021
Editada: NUR BATRISYIA HANNANI ZAIN AZMI el 10 de Mzo. de 2021
i changed the value of x in workspace but then after run the coding , the value of x in workspace changed to old value . thus i get same graph generated with old value . so how should i do so that the value that i want in workspace can be saved and i can get the graph i want with new value of x?
i already overwrites all of x before.
Walter Roberson
Walter Roberson el 10 de Mzo. de 2021
We need to see your current code.
Jorg Woehl
Jorg Woehl el 10 de Mzo. de 2021
Editada: Jorg Woehl el 10 de Mzo. de 2021
Walter is right; you are defining x in your code, so what you have in the workspace doesn't matter. Without really knowing what you are trying to code, my guess would be that you need to remove at least this line:
x = sin(2*pi*t*100.123e3);
ohh . so what code i need to write to define my own x? i want to find mean frequency for power spectral density .
Walter Roberson
Walter Roberson el 11 de Mzo. de 2021
What is the source of your signal? Is it stored in a .mat file at the moment? Does it need to be read from a .wav ?
yes in mat file .

Iniciar sesión para comentar.

Respuestas (1)

TED MOSBY
TED MOSBY el 25 de Ag. de 2024
I understand that you want to change the value of a variable in the workspace. The problem you are facing is because you are changing the values in the workspace but that is not the right way as the variables in the workspace are generated after the script is executed.
So, changing the values in the workspace won’t help as when you run the script again it will generate the values according to the code and not take the values of the workspace.
So, to change any value of the variable you must modify the script. You can make your code as a function and then call it as shown below:
function plotPeriodogram(x)
nSamp = numel(x);
Fs = 1024e3;
SNR = 40;
rng default
x = x + randn(size(x)) * std(x) / db2mag(SNR);
[Pxx, f] = periodogram(x, kaiser(nSamp, 38), [], Fs);
meanFreq = meanfreq(Pxx, f);
disp(['Mean Frequency: ', num2str(meanFreq)]);
end
plotPeriodogram(myNewData);
Hope this helps!

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Respondida:

el 25 de Ag. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by