Replace entries only in specific column

9 visualizaciones (últimos 30 días)
rsi d
rsi d el 16 de Oct. de 2016
Comentada: dpb el 17 de Oct. de 2016
Hi I have a mat file which has 20 values in its first column I only want to replace entries ONLY in first column
So my uniques values in my first columns is 1,2,3,4,5,6,7,8,9,11,23,34,44,55,56,12,24,29,30
so i want to replace all first column entries where current value is either of 1,2,3,4,5 replace them to 1001 where current vlue is 8,9,11,24 and replace that to 1002 where current vlue is 23,44,55 and replace that to 1003
How can I do this ?
Any suggestions.
I can export to excel but is there a way i can do in mat file only?

Respuestas (2)

dpb
dpb el 16 de Oct. de 2016
Editada: dpb el 17 de Oct. de 2016
"_...can do in mat file only?"_
Sure, see
doc matfile % for all the details
Although unless the file is really big, is probably faster to read the array into memory via load, make the changes in memory and then rewrite with save
But, since you asked... :)
mat=matfile('yourMATfilename','Writable',True); % get matfile object handle
details=whos(mat); % find the skinny on the .mat file object
vname=details.name; % the variable name (if not already known)
vsize=details.size; % and its size
data=mat.(vname)(1:vsize(1),1); % load first column into workspace
ix={[1:5]; [8,9,11,24]; [23,44,55]}; % values to modify
vx=[1001:1003].'; % values to substitute for each group of ix
for i=1:length(ix)
data(ismember(data,ix{i}))=vx(i); % substitute where ix is found with vx
end
mat.(vname)(1:vsize(1),1)=data; % and save the updated column to file
clear mat % close the object
  2 comentarios
rsi d
rsi d el 17 de Oct. de 2016
i tried this but for this part
for i=1:length(ix)
is=data(ismember(data,ix{i}))=vx(i); % substitute where ix is found with vx
end
I am getting an error
Error: The expression to the left of the equals sign is not a valid target for an
assignment.
dpb
dpb el 17 de Oct. de 2016
Yeah, I started out with an index then decided to just store the result directly and then didn't remove the index variable...just delete the is=; the assignment is to the data array. Fixed up the Answer to match...

Iniciar sesión para comentar.


Austin
Austin el 16 de Oct. de 2016
Yes. You could just change the data directly in your where you have it stored as an array, but this can be a hassle if it is a big array.
You can index into the array and change specific elements:
data(2,1)=1001% this replaces the value in the 2nd row, first column with 1001
To do a bunch at once:
data=[1:1:15]';%data is one column.
a=data<=5 & data>=1%this finds the values that are less then or equal to 5 and greater than or equal to 1
data(a)=1001% this swaps all the values a was true for with the value 1001
I would just define the first column as a variable, fix the elements, then replace the first column with the fixed variable; but I think there should be a better way to do this.

Categorías

Más información sobre Workspace Variables and MAT Files en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by