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.
Related
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
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');
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.
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
Assuming we use Matlab, what is the best way to search for a string in a large text file (can be bigger than 1GB)? Reading the entire file into memory would be costly.
You need to look into this:Importing Large text data.
The method would be to use blocks. Load Data in blocks.
It can be done by the range input argument in xlsread. In the range itself, you can specify the columns as well..
Syntax:
num = xlsread(filename,sheet,xlRange)
Example:
filename = 'myExample.xlsx';
sheet = 1;
xlRange = 'B2:C3';
subsetA = xlsread(filename, sheet, xlRange)
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 9 years ago.
Improve this question
When i run my program for 4 iterations there is no problem but if i run it for more than 4 i get the following error
In an assignment A(I) = B, the number of elements in B
and
I must be the same.
for the following line
Corresponding_value_of_x1(i)=x1(f==Lowest_value_of_the_objective_function(i));
Please help.
maxit=5
iga=1
x1=(6.*bin2dec(String_1))./1023
x2=(6.*bin2dec(String_2))./1023
for i=1:1:maxit
f=(x1.^2+x2-11).^2+(x1+x2.^2-7).^2;
%Displaying results from the iteration
i;
Lowest_value_of_the_objective_function(i)= min(f);
Corresponding_value_of_x1(i)=x1(f==Lowest_value_of_the_objective_function(i));
nx1=(6.*bin2dec(New_string(1)))./1023;
nx2=(6.*bin2dec(New_string(2)))./1023;
x1=nx1;
x2=nx2;
end
Corresponding_value_of_x1
It looks like you're trying to find the minimum of a vector, then the corresponding location of that value. min will do all that at once, avoiding the problem you're running into:
[Lowest_value_of_the_objective_function(i) Corresponding_value_of_x1(i)] = min(f);
Note that your error is occurring because the same minimum value is appearing more than once. This code will return the first of those minimum values. If you want different behavior, you'll have to code it.