I want to read data file that includes titles and want to fit regression models to describe relationship between the variables X and Y.
The data file, data.txt, has two columns named X and Y and the data are
X=[32.0 48.5 36.3 42.9 36.5 32.6 34.0 38.4 27.1 27.6 48.4 43.5 38.5 23.7 34.3 28.7 24.1 38.5 44.6 42.7 47.6 20.6 25.8 37.3 30.3 28.8 28.6 23.9 41.2 21.9 45.2]
Y=[45.8 75.8 52.8 70.1 56.4 51.1 48.6 55.8 45.9 45.3 69.9 63.9 60.8 37.1 52.9 47.1 42.3 56.3 70.0 70.8 71.5 30.1 41.1 57.8 48.0 46.4 46.9 38.0 68.3 32.0 68.0]
I tried to read this by the following commands :
fid = fopen('data.txt','r');
dt = fread(fid);
fclose(fid);
dt
but i am not understanding the result it showing.
EDIT:
The structure of the data file is like the following :
X Y
32 45.8
48.5 75.8
36.3 52.8
42.9 70.1
36.5 56.4
if your data file is like:
32.0, 45.8
48.5, 75.8
...
then you can read it as follows:
data = load('file_name.txt');
x = data(:, 1); y = data(:, 2);
Related
I'm new to Matlab and I want to plot a mesh. My coordinates are:
x = [30 34 38 40 44 48 50]
y = [1:5:20]
Z = [9.1 8.5 7.83 7.54 7.07 6.61 6.49 ;
14.5 8.96 8.21 7.71 7.07 6.61 6.4;
13.37 13.4 10.2 9.4 9 7.3 7.9;
12.09 12 12.14 11.96 13.58 14.12 14.311;
14.97 10.77 11.87 12.4 13.62 14.19 14.94]
Ehen I tried to plot it in Matlab it gives following error:
Data point coordinates have inconsistent dimension.
You have indeed inconsistent dimensions as you need 5 elements in y. Also you need a matrix Z, not a vector.
The following should get you starting:
y = [0:5:20]
%// reshape z in case z is a vector
z = reshape(Z,numel(y),numel(x))
figure(1)
%// mesh(x,y,z)
surf(x,y,z) % colored mesh
Check the data sizes and adjust the reshaping according to your needs!
In general, if X and Y are vectors, length(X) = n and length(Y) = m, where `[m,n] = size(Z)v.
In your case:
length(x)
7
length(y)
4
but
size(Z)
1 35
So you need to reshape the Z.
Do:
x = [30 34 38 40 44 48 50]
y = [0:5:20]
Z = [9.1 8.5 7.83 7.54 7.07 6.61 6.49 ; 14.5 8.96 8.21 7.71 7.07 6.61 6.4; 13.37 13.4 10.2 9.4 9 7.3 7.9; 12.09 12 12.14 11.96 13.58 14.12 14.311; 14.97 10.77 11.87 12.4 13.62 14.19 14.94]
Z = reshape(Z,numel(y),numel(x))
mesh(x,y,Z)
I'm plotting a scatter plot and would like to have the a-axis as entry named than index. My data is from MASS in R and looks like this
animals={'Mountain beaver';'Cow';'Grey wolf';'Goat';'Guinea pig';'Dipliodocus';'Asian elephant';'Donkey';'Horse';'Potar monkey';'Cat';'Giraffe';'Gorilla';'Human';'African elephant';'Triceratops';'Rhesus monkey';'Kangaroo';'Golden hamster';'Mouse';'Rabbit';'Sheep';'Jaguar';'Chimpanzee';'Rat';'Brachiosaurus';'Mole';'Pig'};
body=[1.35 465 36.33 27.66 1.04 11700 2547 187.1 521 10 3.3 529 207 62 6654 9400 6.8 35 0.12 0.023 2.5 55.5 100 52.16 0.28 87000 0.122 192];
brain=[8.1 423 119.5 115 5.5 50 4603 419 655 115 25.6 680 406 1320 5712 70 179 56 1 0.4 12.1 175 157 440 1.9 154.5 3 180];
% Plot
x=1:length(body);
scatter(x,body,'filled','d')
hold
scatter(x,brain,'filled')
legend('body', 'brain','location','east');
How could I amend the program so my scatterplot display animals in xticks with 45 degrees?
I think this is what you want:
% add these lines at the end of your code
set(gca,'XTickLabel',animals)
set(gca,'XTick',1:numel(animals));
xlim([0 numel(animals)+1]);
set(gca, 'XTickLabelRotation', 45);
which gives this:
I'm working on an assignment where I have to read a tab delimited text file and my output has to be a matlab structure.
The contents of the file look like this (It is a bit messy but you get the picture). The actual file contains 500 genes (the rows starting at Analyte 1) and 204 samples (the columns starting at A2)
#1.2
500 204
Name Desc A2 B2 C2 D2 E2 F2 G2 H2
Analyte 1 Analyte 1 978 903 1060 786 736 649 657 733.5
Analyte 2 Analyte 2 995 921 995.5 840 864.5 757 739 852
Analyte 3 Analyte 3 1445.5 1556.5 1579 1147.5 1249 1069.5 1048 1235
Analyte 4 Analyte 4 1550 1371 1449 1127 1196 1337 1167 1359
Analyte 5 Analyte 5 2074 1776 1960 1653 1544 1464 1338 1706
Analyte 6 Analyte 6 2667 2416.5 2601 2257 2258 2144 2173.5 2348
Analyte 7 Analyte 7 3381.5 3013.5 3353 3099.5 2763 2692 2774 2995
My code is as follows:
fid = fopen('gene_expr_500x204.gct', 'r');%Open the given file
% Skip the first line and determine the number or rows and number of samples
dims = textscan(fid, '%d', 2, 'HeaderLines', 1);
ncols = dims{1}(2);
% Now read the variable names
varnames = textscan(fid, '%s', 2 + ncols);
varnames = varnames{1};
% Now create the format spec for your data (2 strings and the rest floats)
spec = ['%s%s', repmat('%f', [1 ncols])];
% Read in all of the data using this custom format specifier. The delimiter will be a tab
data = textscan(fid, spec, 'Delimiter', '\t');
% Place the data into a struct where the variable names are the fieldnames
ge = data{3:ncols+2}
S = struct('gn', data{1}, 'gd', data{2}, 'sid', {varnames});
The part about ge is my current attempt but its not really working. Any help would be very appreciated, thank you in advance!!
A struct field can hold any datatype including a multi-dimensional array or matrix.
Your issue is that data{3:ncols+2} creates a comma-separated list. Since you only have one output on the left side of the assignment, ge will only hold the last column's value. You need to use cat to concatenate all of the columns into a big matrix.
ge = cat(2, data{3:end});
% Or you can do this implicitly with []
% ge = [data{3:end}];
Then you can pass this value to the struct constructor
S = struct('gn', data(1), 'gd', data(2), 'sid', {varnames}, 'ge', ge);
I have a very basic table
Alcohol Tobacco
6.47 4.03
6.13 3.76
6.19 3.77
4.89 3.34
5.63 3.47
4.52 2.92
5.89 3.20
4.79 2.71
5.27 3.53
6.08 4.51
4.02 4.56
I have tried reading it in using textscan but get blank.
fileID = fopen('TabaccoAlcohol.txt');
C_text = textscan(fileID,'%n',2);
It would be nice in the program using the headings as objects, e.g. Alcohol would be all 11 rows of data. I know Matlab can do this but I can't make it work. Please help.
Use readtable:
>> t = readtable('data.txt')
t =
Alcohol Tobacco
_______ _______
6.47 4.03
6.13 3.76
6.19 3.77
4.89 3.34
5.63 3.47
4.52 2.92
5.89 3.2
4.79 2.71
5.27 3.53
6.08 4.51
4.02 4.56
>> t.Alcohol
ans =
6.4700
6.1300
6.1900
4.8900
5.6300
4.5200
5.8900
4.7900
5.2700
6.0800
4.0200
You can change your code with this code is given below
fileID = fopen('read.txt');
C_text = textscan(fileID,' %f %f');
fclose(fileID);
This is a tab-delimited txt that looks like
A 2000 a B 2001 b C 1999 c
X 2005 x Y 1995 y
There will be hundreds of rows like these.
I want to create matrices like
mat1 = [A 2000 a ; B 2001 b ; C 1999 c]
mat2 = [X 2005 x ; Y 1995 y]
I think this is doable if I just use for and if and so on.
But I am not sure if I can do it by some more efficient matlab command using block transpose.
id = fopen('input.txt');
tline = fgetl(id);
ii = 1;
while ischar(tline)
cell_{ii} = cellfun(#(x)(str2double(x)),reshape(regexp(tline,'\t','split')',3,[])');
ii = ii+1;
tline = fgetl(id);
end
fclose(id);
The output matrices are stored in the cell array , cell_
input file is
564 564 5646 65466 458 545
785 878 8745 555 547 5665 55966 66588 6654
output is
cell_{1}
ans =
564 564 5646
65466 458 545
cell_{2}
ans =
785 878 8745
555 547 5665
55966 66588 6654