I want to read data from .txt file to plot a 3D graph in matlab. The data looks like this
T_hor T_ver V_hor V_ver
8,833 -15,43 -11,871 23,604
3,121 -22,78 -9,949 41,712
-8,012 -26,28 -4,317 33,790
-12,697 -20,99 6,948 22,314
-11,960 5,68 2,079 0,469
4,279 -22,17 -10,002 39,791
Each column I've imported as a separate column Vector say T_hor,T_ver,V_hor,V_ver. Now I want to read first 4096 rows of this individual column vector 'T_hor' and then the next 4096 rows of the column vector T_hor. This applies for every column. My Goal was to compute FFT for the 4096 set of values incrementally and store them as column vectors.I was previously using this command to read the values into different column vector.
x1 = T_ver(1:4096);
x2 = T_ver(4097:8193);
I want to make the code look more Logical and avoid unnecessary lines of code. So I tried applying for loop for this but I think I'm doing it in a wrong way.
for X
X = (1:LastValue:4096);
end
I think Vectorization can be more easy and consumes less execution time. Can anyone give me a direction or hints on how to implement this.
Related
I need your help again :). I'm trying to plot multiple lines for a very large dataset. To start easier, I divided the dataset to get a TABLE in Matlab that contains 6 columns, with the first column representing the date that I want on my x-axis. Now I want to plot the other columns (and in the original file are a lot more than 6 columns) on the y axis, using a for loop. I tried the following, with no success:
hold on
for i=2:1:6
plot(Doldenstock(:,1), Doldenstock(:,i));
end
hold off
As I understand this, this code would do exactly what I want for columns 2,3,4,5,6. However, I always get the same error code:
Error using tabular/plot
Too many input arguments.
Error in Plotting_bogeo (line 6)
plot(Doldenstock(:,1), Doldenstock(:,i));
Now, I don't know if maybe for loops like this don't work for tabes but only for arrays?
Thanks for your help in advance!
Cheers,
Tamara
The function plot(x) expect x to be a scalar, a vector, or a matrix. But in your case the input is a table, because accessing a table with parentheses return a table, which is not supported.
If you read the doc "how to access data in a table" you will figure out that you need to use curly brace {} to extract the raw data (in your case a 1D matrix).
So use:
plot(T{:,1},T{:,2})
I simply want to import a matrix from a .csv into Matlab and find that Matlab is acting differently wrt length of a row in my csv. :
First, I read a file of 2 rows with 50000 columns and Matlab correctly shows a 2*50000 matrix in my workspace.
Now, if the file consists of 2 rows with 100000 columns, Matlab identifies it as a 200000*1 matrix.
What has gone wrong there?
What command are you using? csvread('filename.csv') ?
I would personally prefer to use
Data = importdata( 'filename.csv','\t');
I'm trying to finish a program and for some reason, the matrix I loaded into Matlab is messing with the ability to select the rows inside it. I'm trying to select all the rows in the matrix and see which values match the criteria for a Live setting. However I can select specific values/sections of the matrix in the command window without issue. Why is this happening? Any ideas?
It appears to only happen when in a for loop, I can do it just fine when it's on its own.
The syntax is: for x = start:stop. I think you are trying to do a for to the whole "A" matrix. You can split "A", according to its format (e.g. if is a table split in two variables).
bye
Richardd is right on; you're trying to iterate on a matrix, no good.
If I read you right, you're trying to run through your A matrix one column at a time, and see all the rows in that column? Assuming that is correct...
Your A matrix is 14x3, so you should go through your for loop 3 times, which is the size of your column dimension. Luckily, there is a function that MATLAB gives you to do just that. Try:
for iColumn = 1:size(A,2)
...
end
The size function returns the size of your array in a vector of [rows, columns, depth...] - it will go as many dimensions as your array. Calling size(A,2) returns only the size of your array in the column dimension. Now the for loop is iterating on columns.
I am a beginner in Matlab and have not been able to find an answer to my question so far. Your help will definitely be very much appreciated.
I have 70 matrices (100x100), named SUBJ_1, SUBJ_2 etc. I would like to create a loop so that I would calculate some metrics (i.e. max and min values) for each matrix, and save the output in a 70x2 result matrix (where each row would correspond to the consecutively named SUBJ_ matrix).
I am struggling with both stages - how to use the names of individual variables in a 'for' loop and how to properly save individual outputs in a combined array.
Many thanks and all the best!
Don't use such variable names, create a big cell array named SUBJ and put each Matrix in it.
r=zeros(numel(SUBJ),2)
for idx=1:numel(SUBJ)
r(idx,1)=min(min(SUBJ{idx}))
r(idx,2)=max(max(SUBJ{idx}))
end
min and max are called twice because first call creates maximum among rows, second call among columns.
Even though this is in principle possible in Matlab, I would not recommend it: too slow and cumbersome to implement.
You could instead use a 3-D matrix (100x100x70) SUBJ which would contain all the SUBJ_1 etc. in one matrix. This would allow you to calculate min/max etc. with just one line of code. Matlab will take care of the loops internally:
OUTPUT(:,1) = min(min(SUBJ,[],1)[],2);
OUTPUT(:,2) = max(max(SUBJ,[],1)[],2);
Like this, OUTPUT(1,1) contains min(min(SUBJ(:,:,1))) and so on...
As to how to use the names of individual variables in a 'for' loop, here gives an example:
SUBJ = [];
for idx = 1:70
term = eval(['SUBJ_',num2str(idx)]);
SUBJ = [SUBJ; max(max(term)),min(min(term))];
end
I'm trying to write data to a text file from my matlab script. I want two columns which it gives me but I want my time variable (t) followed by my variable (x) which is my amplitude. Its outputting a file like below.
Everything is perfect however i don't believe my (t) variable is first followed by my (x) variable. I'm trying to upload this file to ploy.ly to be graphed but when i set column 1 to my x variable and column 2 to my y variable it plots a oval like below. It's suppose to plot a sinusoidal signal.
My code is as follows:
f = 1E3;
T = 1/f;
tmin = 0;
tmax = 5*T;
dt = T/100;
t = tmin:dt:tmax;
x = sin(2*pi*f*t);
sinData.txt = fopen('sinData.txt','w');
fprintf(sample.txt,'%7.5f,%7.5f\r\n',x);
fclose(sinData.txt);
plot(t,x,'r');
grid on;
MATLAB writes files in column major format. What this means is that it will traverse every row first in one column before moving to the next column. If you have a M x 2 matrix of values where the first column is a vector of t values and the second column is a vector of x values, try transposing the matrix before writing to file. That way, each column represents a pair of (t,x) values for you and MATLAB should be able to preserve writing to the file so that each row is a unique pair.
Also, in your code you are only writing the x values to the file. This makes sense because if you look at your text file going line by line, it pretty much mimics a sinusoidal output. It goes from 0 up to 1, then back to 0 again. Your x vector is a sinusoidal output which thus fits the pattern. As such, you probably want to write the t values as well. To do this, modify the fprintf statement in your code so that it looks like this:
fprintf(sinData.txt,'%7.5f,%7.5f\r\n', [t;x]);
BUG SPOT FROM RTL (Thanks!): Make sure you change sample.txt to sinData.txt in your fprintf statement as that is the name of the handle to the open file that you have opened.
Looking at your code, t and x are row vectors, so this should (hopefully) work. I haven't tried it myself, and I'm somewhere where I don't have MATLAB accessible. Hit me up with a comment and let me know if it works.