Accessing table data in different forms Matlab - matlab

I have T table as follows.
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
FirstName = {'Amanda' ;'Brenda';'Carl'; 'Denis'; 'Ethan'};
Something = {'String1' ;'String2';'String2'; 'String1'; 'String5'};
Weight = [176;163;131;133;119];
FavoriteColor = {'blue' ;'red' ;'yellow'; 'orange' ;'colorblind' };
T = table(Age,FirstName,Weight,FavoriteColor,Something,'RowNames',LastName)
T.FavoriteColor= categorical(T.FavoriteColor);
T.Something= categorical(T.Something);
when I use
A=T(:,5);
I get a variable A which is a table as well. But when I use
A=T.Something;
I get a variable A which is a categorical value like the ones is column Something. I want to use loops so I need to use the first one with indices but I want the result in the second one. What should I do?

Using a=T{:,5}; solved the problem.

Related

How to label structure array elements in an additional field?

I have a 1800x1 structure array with 5 fields. In the field "trial" I`ve stored 14 different numbers which are indicative for a certrain trial characteristica. So for example if 1 stands for rewarded trial and 2 stands for non rewarded trial, I want to add another field which tells me the labels of the respective other field. Any ideas about how to do that?
Assuming you have this data:
a = num2cell(randi(3,15,1));
strings = {'Laurie','rewarded trial','yada yada'};
s = struct('trail',a,'name',[]);
where the value in s(k).trail is the index from strings to be assigned to s(k).name, you can write:
s = struct('trail',a,'name',strings(cell2mat({s.trail})).');
Alternatively, you can do it with a loop:
for k = 1:size(s,1)
s(k).names = strings{s(k).trail};
end

How to filter and save each variable on matlab

I have a data (matrix) with 3 columns : DATA=[ID , DATE, Value]
I want to filter my data by ID for example DATAid1= DATA where ID==1 and so on ..
for that I write this code in MATLAB
load calibrage_capteur.mat
data = [ID ,DATE , Valeur]
minid = min(data(:,1));
maxid = max(data(:,1));
for i=minid:maxid
ind=find(data(:,1) == i)
dataID = [ID(ind) ,DATE(ind) , Valeur(ind)]
end
As a result he register the last value in this example the max ID=31 so he register dataId31. Now I need how to save the variable each iteration. How can I do this?
You will want to use a cell array to hold your data rather than saving them as independent variables that are named based upon the ID.
data_by_ID = cell();
ids = minid:maxid;
for k = 1:numel(ids)
data_by_ID{k} = data(data(:,1) == ids(k),:);
end
Really though, depending on what you're doing with it, you can use data all of the time since all operations are going to be faster on a numeric matrix than they are on a cell array.
%// Do stuff with data ID = 10
do_stuff(data(data(:,1) == 10, :));
Update
If you absolutely must name your variables you could do the following (but please don't do this and use one of the methods above).
for k = 1:numel(ids)
eval(['dataId', num2str(ids(k)), '= data(k,:);']);
end
Your question is a bit unclear but it sounds like you simply want to save the result at each iteration of the for loop.
I'm assuming min and max id are arbitrary and not necessarily the variable you are trying to index on.
kk = min_id:max_id;
dataID=nan(size(kk));
for ii = 1:numel(kk)
ind=find(data(:,1) == kk(ii))
dataID(kk) = [ID(ind) ,DATE(ind) , Valeur(ind)]
end
This is better than indexing by min_id or max_id since it isn't clear that min_id starts at at 1 (maybe it starts at 0, or something else.)

Printing the calculated distance in SQLAlchemy

I am using Flask-SQLAlchemy with Postgres, Postgis and GEOAlchemy. I am able to sort entries in a table according to a point submitted by the user. I wonder how I could also return the calculated distance...
This is how I sort the items:
result = Event.query.order_by(func.ST_Distance(Event.address_gps, coordinates_point)).paginate(page, 10).items
for result in results:
result_dict = result.to_dict()
return result_dict
according to the users position (coordinates_point). I would like to add an entry in each result in the result_dict which also contains the distance that the item was ordered by. How do I do that? What does func.ST_Distance return?
I tried to add this in the for loop above:
current_distance = func.ST_Distance(Event.address_gps, coordinates_point)
result_dict['distance'] = current_distance
But that did not seem to work.
You can use column labels
query = Event.query.with_entities(Event, func.ST_Distance(Event.address_gps, coordinates_point).label('distance')).order_by('distance')
results = query.paginate(page, 10).items
for result in results:
event = result.Event
distance = result.distance
result_dict = event.to_dict()
result_dict['distance'] = distance

Find and replace in matlab?

I want do find and replace all in matlab (As we do in MS office).
https://www.dropbox.com/s/hxfqunjwhnvkl1f/matlab.mat?dl=0
I have a cell array LUT_HS_complete (contains identifier in column 1 and protein name in column 2 and summary in column 3) this is my look up table. on the other hand, I have my protein-protein interaction data (named Second_layer with identifiers in first two columns and the score in column 3).
I want to replace the first two columns in my Second_layer with the corresponding protein name from my look up table.
I tried strmatch, but that didn't help me.
Source_gene = Second_layer(:,1); Source_gene = regexprep(Source_gene,'[-/\s]','');
Target_gene = Second_layer(:,2); Target_gene = regexprep(Target_gene,'[-/\s]','');
Inter_score = Second_layer(:,3);
%%
for i=1:length(Source_gene(1:end,1));
SG = strmatch(Source_gene(i),LUT_HS_complete(1:end,1),'exact');
renamed_Source_gene(SG,1) = LUT_HS_complete(SG,2);
end
for j=1:length(Target_gene(1:end,1));
TG = strmatch(Target_gene(j),LUT_HS_complete(1:end,1),'exact');
renamed_Target_gene(TG,1) = LUT_HS_complete(TG,2);
end
If you could find a solution. It would be a great help.
Might this work for you?
renamed_Second_layer(:,1)=LUT_HS_complete(cellfun(#(x) find(strcmp(x,LUT_HS_complete(:,1))),Second_layer(:,1)),2);
renamed_Second_layer(:,2)=LUT_HS_complete(cellfun(#(x) find(strcmp(x,LUT_HS_complete(:,1))),Second_layer(:,2)),2);
renamed_Second_layer(:,3)=Second_layer(:,3);

retrieve data from structure when entering one field value MATLAB

I have created a structure about patient. Its fields are name, age, etc. I want to be able to retrieve a patient's all info by entering its name or any other unique property. In other words, how can I find the patient's index? Thanks in advance.
patient(10).name = 'Chuck';
patient(10).age = 29;
patient(11).name = 'Sarah';
patient(11).name = 28;
Structures are not a good data type for doing what you want.
I suggest using a table. If you have your data in the structure already, call
patientTable = struct2table(patient);
Then, you get the index as:
chucksIndex = find( strcmp( patientTable.name, 'Chuck'))
And you get that patient's information as
patientTable(chucksIndex,:)
(note: if all you need the index for is lookup, you don't need to call find, the logical index from strcmp suffices).
Double-clicking the table in the workspace browser presents the data in an Excel-like fashion. The only thing you'll have to change in your code is accessing the data. Instead of
patient(10).name
patient(10).age
You write
patientTable.name{10}
patientTable.age(10)
If you do not want to use a table, you can get the index as:
chucksIndex = find(strcmp({patient.name},'Chuck'));
First, there is an error, it should be patient(11).name = 28;.
Here is a way this should work. I've started the indexes for Chuck and Sarah at 1.
patient(1).name = 'Chuck';
patient(1).age = 29;
patient(2).name = 'Sarah';
patient(2).age = 28;
% Assign to cell array
plist = {patient.name};
% Choose the name of a patient
choose_patient = 'Sarah';
% Find the chosen patient in the list
ix_patient = find(strcmp(plist, choose_patient))
% Get full entry for that patient
patient(ix_patient)