How I can use Unique command to remove duplicates but still can use the data of the removed duplicates?

조회 수: 1 (최근 30일)
I have an excel file that has strings of species name and their Antoine's coefficients. I am willing to import the data into a pop up menu in GUI without having the duplicated strings (Species). However, I would still need to use the data of duplicated string to solve for Antoine's coefficients because they vary according to the min and max temperature (and that is why there are duplicates). I attached the excel file I am willing to use, and the code I started with. Note that the code must be able to identify any additional compound in the excel sheet.
[num,text]=xlsread('Antoine_Coefficient.xlsx');%import the data from excel sheet and store them in a cell array of number and text
N=length(text)-2;%Specifying the length of the text cell array(the substraction of 2 is due to the exta cells)
global compound; %make compound global so it is reused in callbacks
compound=text(3:N+2,1);%assign the name of the given compounds(2 is added after being substracted to update the cell infomration and not confuse it with Antoine's coefficient.
global A B C Tmin Tmax %Make All Antoins' coefficinet global in order to reuse them when calledback
A=num(1:N,1);%assign the A coefficient from the excel file
B=num(1:N,2);%assign the B coefficient from the excel file
C=num(1:N,3);%assign the C coefficient from the excel file
Tmin=num(1:N,4);%assign the Tmin from the excel file
Tmax=num(1:N,5);%assign the Tmax from the excel file
set(handles.popupmenu1,'String',compound);%adding the compounds to the pop up menu in the form of a string
  댓글 수: 3
Jan
Jan 2016년 11월 29일
Please post the relevant part of the code only. It is a waste of time, if all users try to locate the concerned lines. Details like "Antoine's coefficients" are confusing only. For this problem they are strings or array. So what is your input exactly and what is the wanted output?
Khalil Ishaq
Khalil Ishaq 2016년 11월 29일
편집: Khalil Ishaq 2016년 11월 29일
I am trying to apply the unique command in order to remove the duplicate compounds ; however, I still want the coefficients to be in order with the compounds, so later I can set an if statement that asks the user to choose the temperature range of the duplicated compounds (if selected).

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2016년 11월 29일
편집: Jan 2016년 11월 29일
Do not use globals to share data between callbacks. Use either the ApplicationData (e.g. as by guidata) or the 'UserData' of e.g. the figure.
[uC, iA, iC] = unique(compound);
handles.Compounds.iA = iA;
handles.Compounds.iC = iC;
handles.Compounds.Coeff = rand(1, numel(compound)); % ???
handles.Compounds.Name = uC; % Stored as 'String' also in the next line:
set(handles.popupmenu1, 'String', uC);
...
guidata(FigureHandle, handles); % Update the handles struct
Then in the callback:
function popup1Callback(PopMenu1H, EventData)
handles = guidata(PopMenu1H);
index = get(PopMenu1H, 'value');
string = handles.Compounds.Name{index}
origIndex = handles.Compounds.iC(handles.Compounds.iA(index));
origMatch = (handles.Compounds.iC == origIndex);
origCoeff = handles.Compounds.Coeff(origMatch)
  댓글 수: 1
Guillaume
Guillaume 2016년 11월 29일
편집: Guillaume 2016년 11월 29일
"Do not use globals." Indeed, they make the lifetime of variables very difficult to track and will lead to bugs.
Even worse than globals are globals named A, B and C, names that have absolutely no meaning and make understand the code an hopeless task (speaking from experience here, having had to debug/refactor such awful code).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by