Matlab - fft form ascii [closed] - matlab

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to represent a FFT based on measurements i have saved on a file.
The file is in the format [frequency, amplitude] i.e.
0,00;0,15;
3,91;0,34;
7,81;0,60;
11,72;1,66;
15,63;3,66;
19,53;0,98;
23,44;0,60;
27,34;0,44;
31,25;0,35;
35,16;0,29;
39,06;0,25;
42,97;0,22;
46,88;0,20;
How can i plot those data?

The problem when reading this from a file is that it uses commas instead of points to separate the decimals. To avoid any issues with this, you can read the content of the file as text (leading to one string variable) and replace the commas with points in MATLAB:
fileContent = fileread('input_file.txt');
fileContent = strrep(fileContent ,',','.');
Next you can use the sscanf (string scan) function to extract the floating point values (%f) from the string. With [2,inf] you specify you want the output to have two rows and as many columns as needed.
A = sscanf(fileContent ,'%f;%f;\n',[2,inf]);
You then have an array A with the frequencies in the first row and the corresponding values in the second row. With that you can create any plot you like, e.g.
stem(A(1,:),A(2,:));
title('FFT of a signal');
xlabel('Frequency (Hz)');
ylabel('Amplitude');

Related

Dont understand the function of cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f')); at all [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
IND_STRT = 0;
ALL_STRT = IND_STRT:12:510;
cmd_data = zeros(length(ALL_STRT),1); %example: x=zeros(1,21) gives you a 1 by 21 matrix
for ii = 1:length(ALL_STRT) %declare variable ii to run from the row of length
if ~isempty(data{i})
cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
end
end
I need to read the EPS from EnduroSat, however i have difficulty understanding the line cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
Im required to utilised MatLab to code and this particular line have an error and i don't understand why.
Whenever you see a complicated line like this in MATLAB, try to break it up.
% find some indices. These values have been selected by the programmer/data, can't say why.
a=ALL_STRT(ii):(ALL_STRT(ii)+4)
% obtain that articular section of the data
b=data{i}(a)
% convert it to a char data type (characters)
c=char(b)
% scan text, and treat them as float
d=textscan(c,'%f')
% the output is a cell array, we want a matrix instead. Make the cell array into a matrix.
cmd_data(ii) = cell2mat(d)
You can read particularly what each of these do better in their documentation pages, and you can see it work if you put a break-point in the code, and see what each of this parts output when you call it. Learn how to debug, is a very very powerful tool

merge two values into one variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Suppose I have two values like 100 and 80 now I wish to store these 2 values in the memory but with in one variable without creating a array or file handling and the thing is retrieving the same values afterwards at another place
It could be a strange approach, but this allows having a single varaible actually holding two varaibles.
You can create a complex varaible in which the real part is the first varaible and the imaginary part is the second variable.
a=100;
b=80
c=complex(a,b)
You can retrieve the original values using the real and imag functions
a=real(c)
b=imag(c)
Hope this helps.
Qapla'
a=80;
b=100;
c = [a,b]; % array (row)
c = [a;b]; % array (column)
c.a=a;c.b=b; % struct
c = {a,b}; % cell
Several options available.

matlab draw a line using user input values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I want to draw a line using the inputs for values x1,y1 and x2,y2 from the gui edit text box and plot them on the axes.
function
You are trying to convert the graphics handle itself to a number rather than converting the contents of the uicontrol to a number. To get the value, you'll want to use the 'String' property of the uicontrol instead.
x1 = str2double(get(handles.edit1, 'String'));
You will want to do the same for all user-supplied values.

Matlab Classification load dataset [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to load and use a dataset in order to run some algorithms(Neural Networks) in Matlab. I've downloaded a dataset from the internet which has instances and attributes.
I've saved that dataset as a plain text file, and also with the extension .data or .mat. But I am not able to import and use it in Matlab.
How should I do? I also have to define a training and a test set after.
Thank you in advance.
I have to mention I am new to Matlab and trying to study it as a hobby.
You can just load the data by:
data = load('wine.data');
Then, you can split the data to training and testing very easily.
Here, I put 70% data for training and 30% for testing, but you could choose other fraction. 60-40 or 80-20
data = data(randperm(end), :);
traindata = data(1:floor(0.7*size(data, 1)), :);
testdata = data(floor(0.7*size(data, 1))+1:end, :);
In the end, when you want to run the classifier, remember that in this dataset, the first column is the label and the rest are features.

Import text file as a matrix in a matlab script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi everybody I need to automaticly import certain text files stored in my computer as matrices when I run my script in matlab. How do I do that? Thanks
Although the question shows little effort I am reminded how I started out with no knowledge about input or output whatsoever, it is a quite dense forest of information really.
Basically to read a file you need to:
Open the file
Read the file and assign it to a variable
Close the file
Some functions in MatLab take care of all three steps:
importdata
csvread
dlmread
The functions above are suitable if you have very neat and uniform data. Click the links to read if they are suitable for you. If your data is less uniform, e.g. it contains both numbers and letters, you might want to consider textscan.
Using textscan you must carry out all three steps yourself. First open your file and create a link to your file called a file ID (FID):
FID = fopen('mytextfile.txt')
Next you define a format specifier which describes a single line of data (a row).
formatSpec = '%f %f %f %f %s'
This format specifier represents 4 decimal numbers (floats) followed by a string all seperated by whitespace. For more information on the format specifier see:
http://www.mathworks.nl/help/matlab/ref/textscan.html#inputarg_formatSpec
Now you can read your text file by calling:
C = textscan(FID,formatSpec);
Which stores each column in a cell in C. So the first column is C{1}, the second C{2}, etc.
Finally make sure you close your file by using the file id:
fclose(FID);
Good luck!