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 have a script which generates a 2 cell array (x and y coordinates). How can I merge them (two columns), save them in one CSV file so I can read them later in Excel?
Below some simplified code which has two solutions that might help you
x = 1:10;
y = 11:20;
x = num2cell(x);
y = num2cell(y);
x2 = cell2mat(x);
y2 = cell2mat(y);
newmat = [x2;y2]';
csvwrite('output.csv',newmat)
fid = fopen ('output2.csv','w');
for i = 1:length(x)
fprintf (fid,'%f, %f\n',x{i},y{i});
end
fclose (fid);
related to what GameOfThrows mentions this only works if x and y have the same length
Note the first solutions converts the cell to an arra which might not always give the result as shown in the example. The second is a more general one with a formatted output...
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 6 years ago.
Improve this question
I have a set of data in a text file with the format like below. How shall I import the data into MATLAB? Thanks!
{(38.7948,1319,0.8571,119,0),(39.0693,138,0.0897,21,0),(40.7911,63,0.0409,7,0),(103.4512,19,0.0123,5,0),(-26.0424,223,1.0000,28,0)}
{(35.8689,110,0.5093,14,0),(47.7915,41,0.1898,7,0),(59.7489,53,0.2454,7,0),(71.7298,12,0.0556,3,0)}
It's always helpful if you in your question explain what you have tried so far, what the desired result would be or how you inteend to use it. That way the person answering the question don't have to assume a lot of things.
Here I assume that you would like to import each line as a cell with a set of number arrays within.
To get matlab to evalutate the expression correct the parentheses in the brackets need to be replaced
{(1,2),(3,4)}
Error: Expression or statement is incorrect
{[1,2],[3,4]}
ans =
[1x2 double] [1x2 double]
To read the file you could use fopen and then fgetl to get each line. When the result from fgetl isn't a char, the end of the file (EOF) is reached.
% Open file
f = fopen('...path\to\file.txt','r');
C = {};
while true
% Read each line
fStr = fgetl(f);
if ischar(fStr)
% Replace parentheses and evaluate expresission
C{end+1} = eval(regexprep(fStr,{'(',')'},{'[',']'}));
else
% End of file
break
end
end
fclose(f)
Perhaps you need to include some error checks if the data in your file should be formated incorrect. You could also check out other ways to read data, for exmple fread or fscanf
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 6 years ago.
Improve this question
I want to perform integration on a vector L but I don't know exactly what to use. I want to obtain a (the integral) as a vector that is the same size as NT.
clc;clear;
syms x
NT=input('NT=');
L=zeros(NT,1);
for i=1:NT
disp('Longeur de travée')
L(i)=input('L = ');
L(i)=L(i);
fa(i)=L(i).*x^2;
a(i)=int(fa)
end
An easy way would be to use trapz. If you have X and Y such that Y(i) = f(X(i)) (so Y contains the values of some function at the location X) then you simply do
I = trapz(X, Y)
In your case, you can do
I = trapz(L, fa)
I guess, looking at your code.
Note that you could use more advanced techniques, that will, in principle, give you a better result (because they are higher-order). This is just one method, but an easy one.
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 7 years ago.
Improve this question
Please help me out with this code:
for i = 1:n
u(t - a_i - td_i);
end
where:
u: step function
t: time vector with n elements
a_i, tau_i, and td_i: variables that change inside for loop
I guess I need to use zeros and ones, but how can I do this correctly?
Simple function to sum the value inside the loop:
func = #(t) sum(t > a + td)
func(t) will be the sum of the foor loop
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');