I need h help i save value in xlsx format through push button function

1 次查看(过去 30 天)
Hi Dear Sir,
I need h help i save value in xlsx format through push button function. I hope a better code solution forwrd me thanks.
These data value want to save with new function of push button. My code is below:
data = get(handles.uitable1, 'Data');
data{1, 1} = num2str(Contrast(1));
data{1, 2} = num2str(Contrast(2));
data{1, 3} = num2str(Contrast(3));
data{1, 4} = num2str(Contrast(4));
data{1, 5} = num2str(mean(Contrast));
data{2, 1} = num2str(Correlation(1));
data{2, 2} = num2str(Correlation(2));
data{2, 3} = num2str(Correlation(3));
data{2, 4} = num2str(Correlation(4));
data{2, 5} = num2str(mean(Correlation));
data{3, 1} = num2str(Energy(1));
data{3, 2} = num2str(Energy(2));
data{3, 3} = num2str(Energy(3));
data{3, 4} = num2str(Energy(4));
data{3, 5} = num2str(mean(Energy));
data{4, 1} = num2str(Homogeneity(1));
data{4, 2} = num2str(Homogeneity(2));
data{4, 3} = num2str(Homogeneity(3));
data{4, 4} = num2str(Homogeneity(4));
data{4, 5} = num2str(mean(Homogeneity));
global AA1 AA2 AA3 AA4 AA5 AA6 AA7 AA8 meann
AA1 = Mean;
AA2 = Standard_Deviation;
AA3 = Entropy;
AA4 = Variance;
AA5 = Smoothness;
AA6 = Kurtosis;
AA7 = Skewness;
AA8 = IDM;
set(handles.edit3, 'string', AA1);
set(handles.edit5, 'string', AA2);
%set(handles.edit7, 'string', RMS);
set(handles.edit9, 'string', AA3);
set(handles.edit11, 'string', AA4);
set(handles.edit13, 'string', AA5);
set(handles.edit15, 'string', AA6);
set(handles.edit17, 'string', AA7);
set(handles.edit19, 'string', AA8);
handles.saved = data;
set(handles.uitable1, 'Data', data)
guidata(hObject, handles);
  4 个评论
Jan
Jan 2022-7-13
@Tanveer Aslam: Please answer Rik's questions for clarifications. This would allow to understand your code and to post amatching solution.
Tanveer Aslam
Tanveer Aslam 2022-7-13
编辑:Rik 2022-7-13
Global variable used for pass by value. Here i write complete code push button function. Secondly push button at the used for save data in xlsx by pass value. Error in second button when data save.
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
img2 = handles.img1;
pixel_dist = str2double(get(handles.edit1, 'string'));
GLCM = graycomatrix(img2, 'Offset',[0 pixel_dist; -pixel_dist pixel_dist; -pixel_dist 0; pixel_dist -pixel_dist; -pixel_dist -pixel_dist]);
stats = graycoprops(GLCM, {'Contrast', 'Correlation', 'Energy', 'Homogeneity'});
Contrast = stats.Contrast;
Correlation = stats.Correlation;
Energy = stats.Energy;
Homogeneity = stats.Homogeneity;
Mean = mean2(img2);
Standard_Deviation = std2(img2);
Entropy = entropy (img2);
%RMS = peak2rms(img2);
Variance = mean2(var(double(img2)));
a = sum(double(img2(:)));
Smoothness = 1 - (1/(1+a));
Kurtosis = kurtosis(double(img2(:)));
Skewness = skewness(double(img2(:)));
%Inverse Difference Movement
m = size(img2, 1);
n = size(img2, 2);
in_diff = 0;
for i = 1:m
for j = 1:n
temp = img2(i,j)./(1+(i-j).^2);
in_diff = in_diff+temp;
end
end
IDM = double(in_diff);
data = get(handles.uitable1, 'Data');
data{1, 1} = num2str(Contrast(1));
data{1, 2} = num2str(Contrast(2));
data{1, 3} = num2str(Contrast(3));
data{1, 4} = num2str(Contrast(4));
data{1, 5} = num2str(mean(Contrast));
data{2, 1} = num2str(Correlation(1));
data{2, 2} = num2str(Correlation(2));
data{2, 3} = num2str(Correlation(3));
data{2, 4} = num2str(Correlation(4));
data{2, 5} = num2str(mean(Correlation));
data{3, 1} = num2str(Energy(1));
data{3, 2} = num2str(Energy(2));
data{3, 3} = num2str(Energy(3));
data{3, 4} = num2str(Energy(4));
data{3, 5} = num2str(mean(Energy));
data{4, 1} = num2str(Homogeneity(1));
data{4, 2} = num2str(Homogeneity(2));
data{4, 3} = num2str(Homogeneity(3));
data{4, 4} = num2str(Homogeneity(4));
data{4, 5} = num2str(mean(Homogeneity));
global AA1 AA2 AA3 AA4 AA5 AA6 AA7 AA8 q;
AA1 = Mean;
AA2 = Standard_Deviation;
AA3 = Entropy;
AA4 = Variance;
AA5 = Smoothness;
AA6 = Kurtosis;
AA7 = Skewness;
AA8 = IDM;
set(handles.edit3, 'string', AA1);
set(handles.edit5, 'string', AA2);
%set(handles.edit7, 'string', RMS);
set(handles.edit9, 'string', AA3);
set(handles.edit11, 'string', AA4);
set(handles.edit13, 'string', AA5);
set(handles.edit15, 'string', AA6);
set(handles.edit17, 'string', AA7);
set(handles.edit19, 'string', AA8);
%data =xlswrite('uitable1');
handles.saved = data;
set(handles.uitable1, 'Data', data)
guidata(hObject, handles);
%% Here secondly push button used for data save in xlsx format. Here guide me how data pass and push button for save value in excel.
% --- Executes on button press in SAVE.
function SAVE_Callback(hObject, eventdata, handles)
% hObject handle to SAVE (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%data = get(handles.UITable, 'Data');
global q
myData = q;
% write your data to file
xlswrite('myFile.xlsx', myData);

请先登录,再进行评论。

采纳的回答

Rik
Rik 2022-7-13
Because you used global variables instead of sharing data through the guidata struct, it is impossible to know what went wrong. You also used an extremely short name, which increases the chance of a collision with some other function that might be poorly designed and made the same mistake.
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread. That will also show you how to share data between callbacks.
There is nothing fundamentally wrong with your xlswrite call. The problem is with the data sharing. You're also using numbered variables, which tend to be a sign you're not using arrays effectively.
However, for your specific example it is actually quite simple: your first callback does not assign any value to the global q variable, so the second callback will try to write whatever the last function that happened to use q as a global assigned to it.
  2 个评论
Tanveer Aslam
Tanveer Aslam 2022-7-13
Thanks for answer. i follow it and try my best.
Hope in feature you guide me. I wait your email.
my email: tanveerchuhan786@gmail.com
Rik
Rik 2022-7-13
Glad to be of help.
I don't do private consulting. This forum is a hobby for me. If you want private help, there are many people who would be happy to help you for a fee.
If you want my help, it will be on this forum in public, and without any guarantees. Feel free to tag me in a new post or post the link in a comment here.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by