how can I define an excel woorksheet in Matlab that consider the location of cells with equal numeric value and change them into specific values that I want.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
how can I define an excel worksheet in Matlab that consider the location of cells with equal numeric values and change them into specific values that I want. in other word I want : Region 1 = locations of all cells with numerical values of A Region 2 = locations of all cells with numeric value of B And so on. and then change each region to specific values that I want. and repeat it and each time changing region s first value to specific values, as many times that I want . each region has so many cells therefor I cant define them cell by cells in matlab.
Accepted Answer
dpb
on 16 Mar 2014
It's so trivial in Matlab; just import the array; make the changes and rewrite (assuming you must use Excel at all; why can't you just use Matlab?)
data=xlsread('yourfile.xls');
data(data==A)=newA;
data(data==B)=newB;
xlswrite('yourfile.xls',data)
You may have the problem depending on the values addressed in the FAQ--
13 Comments
"why can't you just use Matlab?"
Because some people stick to Excel, the tool they are comfortable with, and refuse to actually use a more powerful tool unless absolutely forced. Moving away from a comfort zone can be difficult.
bahareh's so-called "Answer" moved here:
I don't want to define a value for it , I mean I don't want to say ( data==A), I want that matlab know the location of cells with variable A and change it to value that I want. ( because it is a part of a program and I cant define a value for each region manually). my problem is how can I define to matlab , the location of all cells of with variable A? I have a worksheet with thousands of cells, and in my program I want to change each region with same values , to specific value by program.
data==A does not define data to be A. That would be if you used a single equals sign. data==A gives you the locations as a logical array, just exactly like you're asking for.
dpb
on 16 Mar 2014
@John--yeah, I know, the ? is mostly rhetorical w/ the idea of perhaps encouraging the move to the "more powerful tool" :)
@bahareh -- which is exactly the solution I gave you with the caveat that you may need to deal w/ FP comparisons as noted.
baran
on 16 Mar 2014
because the program run many times and A isn't constant and change each time , and each time I want to change the first cells with variable A, that maybe now have different value
baran
on 16 Mar 2014
because the program run many times and A isn't constant and change each time , and each time I want to change the first cells with variable A, that maybe now have different value, what can I do ????
dpb
on 16 Mar 2014
Me no follow -- where's the problem in executing the above whenever needed for whatever value(s) of A and B desired?
Where is A being defined?
baran
on 16 Mar 2014
A define in the another program .because the program run many times if I define (data==A) , the second time the program run ,( A) change to another stochastic values, for example newA . then may be newA=newB . and it is the problem in executing the above .
Well, you're going to have that problem no matter what it would seem. Retrieve A from that source.
You started out by asking how Matlab could do something with Excel--we've given you the logical way to do that and you do nothing but continue to find fault.
Give a better overall picture of the problem -- where/how does Matlab enter into it that you asked the question in a Matlab forum to begin with? And, what's the sequence by which this "other program" gets executed and what is it?
We can only respond as best we can to questions as asked--we don't "know nuthink" about the rest of the process.
And saying "can't" isn't any more helpful in describing the reason why you see a problem and what that problem is for getting useful help in solving whatever problem it is than is "doesn't work" as the only complaint for a piece of code without context of what, specifically, doesn't work means in the given case.
baran
on 18 Mar 2014
thanks so much for your helping. you are right . I explain my problem partially. here I explain it again and I appreciate you for your help. I have a worksheet excel file that it should be link with matlab and my program. the data in it analyze with the program and then back to the excel file as a new data . in my excel file I have too many cells, each cell has value . I want matlab to classify these cells as some regions : so for example : region 1= ( all cell with value A for example 1 ). region 2 =( all cells with value B for example 2)and so on others region like this. then these regions with these values will be define in my program and analyze and replace with new values associate to each region . and it is repeated over and over again . Since the numerical value of the two regions after several iterations may be equal,I don't want that matlab classify them based on new values, I want the first classification stay unique, and each time cells give new value based on first defined rigions. how should I define these region to matlab to consider them as I define them first time and each time associate appropriate value to them ? is it possible at all? I hope I can explain it well. thanks so much
Well, just save the indices (logical array) the first iteration -- keep the logical array and use it instead of implicitly assigning as my first solution does...
data=xlsread('yourfile.xls'); % read the data
idxA=data==A; % find the matching (remember FP issues)
data(idxA)=newA; % make the initial substitution
You'll need a logic flag for "first pass" that you pass to the function to determine whether to do the search again or not. Sotoo
data=xlsread('yourfile.xls'); % read the data
if firstflg, idxA=data==A; end % find the matching (remember FP issues)
data(idxA)=newA; % make the substitution
If this in in a script the variables will remain in the workspace.
baran
on 18 Mar 2014
thanks so much , if "A" has value , for example all cells with value 2 , that when excel link to matlab "2" go to functions and change to another value and 2 in my program is a number and it is not symbol, then is it still true ? and what do you mean " if firstflag ..." can you explain more ? thanks.
dpb
on 18 Mar 2014
I don't know about how you're doing the "Excel link to Matlab" specifically, but certainly you'll have to fix any issue of whether the value passed is the character representation (a string/character variable) or the value itself. That should be relatively simple to deal with.
The "firstflg" is a variable you set to know whether the index array needs to be updated or whether to use the previously computed one. It's a local variable to Matlab controlled by whoever is in charge of the overall process which you've yet to describe how this whole thing is structured.
More Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)