How to read .csv file from a different location in MATLAB? - matlab

I'm new to MATLAB, I've been fiddling around to import a .csv file which is on my desktop to MATLAB. I've already tried csvread() function as shown,
M = csvread(C:/Users/XYZ/Desktop/train.csv)
Please help. Thanks in advance.
EDIT:
My data is in the following format:
Id DV T1 T2
1 1 15 3
2 4 16 14
3 1 10 10
4 1 18 18

The csvread method takes a string (i.e. a char type) for the filename. In matlab a char must be expressed inside quotation marks, so this should work:
M = csvread('C:/Users/XYZ/Desktop/train.csv')

Related

Tableau : Calculed field to transform series of odd numbers to a sequence of numbers

I need your help with the formulation for a calculed a field in Tableau (Tableau Prep to be accurate).
I have a field called [Code Order] which contains only a series of Odd numbers (1,3,5,7,9,..) multiple times, which means it can be (1,3,1,3,5,7,1,1,1,3,5,7,9,11).
What I need is to transform these in a normal sequence of numbers so for my example above I need as a result: (1,2,1,2,3,4,1,1,1,2,3,4,5,6)
In other words when in [Code Order] I have :
1 = 1
3 = 2
5 = 3
7 = 4
9 = 5
11 = 6
13 = 7
15 = 8
...
365 = 183
For the moment my maximum is 365, which is position 183, I would like to avoid to type 182 IF formulas if possible. ;)
Thanks in advance for your help.
CYA
Plt.K
This might turn out to be more accurate in case your Code Order series is missing any values along the way.
Example series:
Alternate Field:
Tableau Setup:
You want to use the index() calculated field. Create a new field called index. The calculation is just index().
Add [Code Order] to your row shelf and index to your label. You should see something like this.
The following calculation should do the trick
CEILING([Code Order] / 2)

How do I use the Arrhenis Equation in Matlab?

I need to use the equation k=k0*e^(-Q/RT) where T is 10 variables between 90 and 500, to generate the 10 variables I used T = linspace(90,500,10), but when I try to generate the equation it wont let me, k0=1200,Q=8000,R=2 so when I type in k=k0*exp(-Q./(R.*T) the numbers are kind of funky with some 0.0000, am I doing something wrong? Thanks
T = linspace(90,500,10);
k0=1200;
Q=8000;
R=2;
k=k0*exp(-Q./(R.*T));
format longG
k =
Columns 1 through 3
5.98693127135402e-17 1.83626027549851e-10 3.07185900119097e-07
Columns 4 through 6
2.60112352637493e-05 0.000498552970093129 0.00409767412987074
Columns 7 through 9
0.0198590098887835 0.067709291730596 0.180526236997812
Column 10
0.402555153483014
results = [T';k'];
Nothing wrong there, just the format that went wrong I reckon. For formatting options, see either the documentation on format, or this question.

create a new matrix from values obtained iterating through other matricies

In Matlab I have 4 matricies which are all 1(row) by 4(coloumns) (ABDC, EFGH, IJKL, MNOP)
Their names are also stored in a list
Stock_List2 = {'ABCD' 'EFGH' 'IJKL' 'MNOP'} and is a 1 by 4 cell.
I want to iterate through the list and create a new matrix called "display" which takes the values of the indvidual matricies and places them underneath each other)
I am trying something like
for e = 1:length(Stock_List2)
display(e) = eval(strcat(Stock_List2)(e))
end
Error: ()-indexing must appear last in an index expression.
However getting the following error expression which truthfully may well just be that I'm way off the mark.
As an example if the orginal matricies are as follows:
ABCD 1 2 3 4
DEFG 5 6 7 8
HIJK 9 8 7 6
LMNO 5 4 3 2
I would like the final output ie the 'display matrix to be a 4 by 4 matrix looking like
display
1 2 3 4
5 6 7 8
9 8 7 6
5 4 3 2
If I understood right you want to concatenate vertically the matrices ABDC, EFGH, IJKL and MNOP saving them in the matrix "display".
You could do:
display = [ABDC; EFGH; IJKL; MNOP]
or:
for i=1:length(Stock_List2)
display(i,:) = Stock_List2{i}
end
Apologies if what I wanted wasnt clear - I've got the following from a colleague which achieves the desired result
for e=1:length(Stock_List2)
eval(strcat('display_mat(e,:) = ',Stock_List2{e}));
end

Problems Using Textscan to Read Multiple Lines

I'm a bit new to data import using Matlab.
Basically, I have an Ascii file. It has 13 Header Lines, along with 765 columns and ~3500 rows of data. I am attempting to import the data into a 3500 x 765 matrix in Matlab. I've tried the following:
fileID = fopen('filename');
formatspec = [repmat('%f ', [1,765])];
raw_data=textscan(fileID,formatspec, 'Headerlines',13,'delimiter','\t');
It successfully skips the 13 header lines. However, it only gives me a 1 x 765 matrix containing only the data from the first row.
Perhaps I have misunderstood just how I am supposed to use textscan, so any help in getting my other ~3499 rows of data would be very well appreciated.
~Thank You
NOTE
The Data File itself is formatted as follows. The First 13 lines do not contain the data itself. All lines following that contain sets of data similar to what will be pasted below, extending for 700+ columns and 3000+ rows.
Wyko ASCII Data File Format 0 1 1
X Size 3571
Y Size 765
Block Name Type Length Value
Wavelength 7 4 72.482628
Aspect 7 4 1
Pixel_size 7 4 0.00196
StageY 7 4 -0.048055
Magnification 8 8 5.05
StageX 7 4 0.214484
ScannerPosition 7 4 3490.000732
ScannerSpeed 7 4 3.165393
RAW_DATA 3 10927260
-10976.61035 -10977.07324 -10981.07422 -10985.6084 ...
-10967.41309 -10963.31836 -10966.75195 -10980.40723 ...
-10969.08496 -10976.03711 -10976.62988 -10964.23731 ...
-10974.12695 -10976.61133 -10979.2627 -10973.57813 ...
-10969.21094 -10966.56543 -10973.74512 -10983.41797 ...
-10970.18359 -10980.82715 -10968.00195 -10975.58594 ...
-10980.41016 -10982.39356 -10982.74316 -10974.51563 ...
-10972.31641 -10984.00488 -10987.89453 -10976.23633 ...
I think the following should work, but I don't have Matlab on this machine to test it out.
fileID = fopen('filename');
formatspec = [repmat('%f ', [1,765])];
raw_data = new_data = textscan(fileID,formatspec, 'Headerlines',13,'delimiter','\t');
while ~feof(fileID)
new_data = textscan(fileID,formatspec,'delimiter','\t');
raw_data = [raw_data; new_data];
end
fclose(fileID);
Note that this is not a particularly efficient way to do it. If your header lines give you the size of your array, you may want to use zeros to create an array of the appropriate size and then read the data into your array.

Arrange data using loop in MATLAB

If I have:
t=(1:1:5)'
time=1:3:100
How do I arrange data t in each column starting from 1 until the end, with an interval of 3. Which means that the data t (1 to 5) at column 1,4,7 and so on.
I've tried:
t=[1:1:5];
nt=length(temp);
time=[1:1:100];
nti=length(time);
x=zeros(nt,nti);
temp=temp';
initiator=2;
monomer=3;
post=1:3:100;
for l=1:post
step=1;
maxstep=100;
while (step<maxstep)
step=step+3;
temp=(1:1:5)';
end
t(:,l)=t;
x=[t];
end
This only shows result X with temp at column 1. I do not know how to to arrange this data at columns that I want.
Hope someone will help me. Thank you in advance.
How many dimensions does your data have? If you already have "temp" (temperature?) and "time" as your first two dimensions and you want "t" to be the third dimension, then create a three-dimension matrix.
To extract from indexes [1 4 7 10 13 16 ... ], use (1:3:end)
To extract from indexed [2 5 8 11 14 17 ... ], use (2:3:end)
In MATLAB's colon notation, the first value is the start. Second value is increment. Third value is the end value and is inclusive.