Need to fix my code

3 次查看(过去 30 天)
Rihard
Rihard 2023-1-7
回答: Dinesh 2023-3-6
Can anyone help me with my code? Why is it running so slowly? Thanks!
clc;
clear all;
close all;
%============ Kasutajaakna tekitamine ===========
h_juur=get(0);
ekraan=h_juur.ScreenSize;
asukoht=[ekraan(3)/5 ekraan(4)/4 ekraan(3)/2+20 ekraan(4)/3+290];
h_joonis=figure('Name','TUULTEROOS [Andmed.xlsx]','NumberTitle','on','ToolBar','none');
h_joonis.Position=asukoht;
h_uimenu=uimenu('Text','Faili import','Callback',@import);
h_uimenu=uimenu('Text','Autor','Callback',@mydialog);
%========== UI menu korrastamine ========
allhandles = findall(h_joonis);
menuhandles = findobj(allhandles,'type','uimenu');
edithandle = findobj(menuhandles,'tag','figMenuEdit');
filehandle = findobj(menuhandles,'tag','figMenuFile');
viewhandle = findobj(menuhandles,'tag','figMenuView');
inserthandle = findobj(menuhandles,'tag','figMenuInsert');
toolshandle = findobj(menuhandles,'tag','figMenuTools');
desktophandle = findobj(menuhandles,'tag','figMenuDesktop');
windowhandle = findobj(menuhandles,'tag','figMenuWindow');
helphandle = findobj(menuhandles,'tag','figMenuHelp');
delete(edithandle);
delete(filehandle);
delete(viewhandle);
delete(inserthandle);
delete(toolshandle);
delete(desktophandle);
delete(windowhandle);
delete(helphandle);
%======= Pildi töötlemine ja lisamine ==========
pilt = imread('Kompass.png','png','BackgroundColor',[.94 .94 .94]);
pilt1 = imresize(pilt, [70 NaN], 'bilinear');
subplot(3, 5, 1.5);
imshow(pilt1);
%======= Tuuleroosi põhja tekitamine + kuude sisselugemine ========
global kuud
kuud = sheetnames('Andmed.xlsx');
h_polaraxes=polaraxes('Parent',h_joonis,...
'Units','characters',...
'Position',[60 -15 80 75],...
'Visible','on',...
'ThetaTick',0:45:360,...
'ThetaTickLabel',({'N','NE','E','SE','S','SW','W','NW'}),...
'ThetaZeroLocation','top',...
'ThetaDir','clockwise');
%======= Nuppude loomine =========
h_popup=uicontrol(h_joonis,...
'Style','popupmenu',...
'Units','characters',...
'Position', [10, 25, 20, 10],...
'String',kuud,...
'Callback',@pup_Callback);
[z jaankp]=xlsread('Andmed.xlsx','Jaanuar','A:A');
jaankp(1,:) = [];
h_listbox=uicontrol(h_joonis,...
'Style','listbox',...
'Units','characters',...
'Position',[10, 20, 20, 10],...
'Max',31,'Min',1,...
'String', jaankp,...
'Visible','on');
h_plot_nupp=uicontrol(h_joonis,...
'Style','pushbutton',...
'Units','characters',...
'Position', [10, 5, 20, 2],...
'String','Uuenda andmeid',...
'Callback',{@plot_nupp_callback,h_polaraxes,h_popup,h_listbox});
h_checkbox = uicontrol(h_joonis,...
'Style','checkbox',...
'Units','characters',...
'Position', [10, 30, 20, 2],...
'String','Kuude kaupa',...
'Callback',{@checkbox_callback,h_listbox,h_popup});
% ========== Radio rühm ==========================
h_radio_ryhm=uibuttongroup(h_joonis,...
'Visible', 'on',...
'Units','characters',...
'Position',[10, 37, 20, 5],...
'SelectionChangedFcn',{@radio_ryhm_callback,h_polaraxes});
h_radio_1=uicontrol(h_radio_ryhm,...
'Style','radiobutton',...
'Units','characters',...
'Position',[0.7,0.7, 15,6],...
'String','8-ilmakaart',...
'HandleVisibility','on',...
'Tag','radio_1');
h_radio_2=uicontrol(h_radio_ryhm,...
'Style','radiobutton',...
'Units','characters',...
'Position',[0.7,0.7, 15,2],...
'String','16-ilmakaart',...
'HandleVisibility','on',...
'Tag','radio_2');
% ========= Jaam ning kuupäevad/kuud =================
jaam=uicontrol(h_joonis,...
'Style','text',...
'Units','characters',...
'Position',[92.5 0.75 15 2],...
'FontSize', 10,...
'String','Jaam',...
'Callback',@lst_Callback);
pealkiri=uicontrol(h_joonis,...
'Style','text',...
'Units','characters',...
'FontSize', 10,...
'FontWeight','bold',...
'Position',[90 43 20 5],...
'String','Kuupäev / kuu',...
'Callback',@lst_Callback);
%============== Callback funktsioonid ============
h_listbox.Callback={@lst_Callback, h_polaraxes, h_radio_1, pealkiri, jaam };
h_popup.Callback={@pup_Callback,h_listbox};
%============== Autori aken =======================
function mydialog(h_uimenu, syndmus)
d = dialog('Position',[300 300 250 150],'Name','Autor');
tekst = uicontrol('Parent',d,...
'Style','text',...
'Position',[20 80 210 40],...
'String','Kinnitan, et olen koodi autor. ©Marti Kütt 2022');
nupp = uicontrol('Parent',d,...
'Position',[85 20 70 25],...
'String','Sulge',...
'Callback','delete(gcf)');
end
%============== Faili import ===============
function import(obj,syndmus)
desktop=winqueryreg('HKEY_CURRENT_USER', 'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', 'desktop');
[FailiNimi,PathName,FilterIndex] = uigetfile({'*.xlsx'},'vali sobiv fail ',desktop)
end
function pup_Callback(h_popup, syndmus, h_listbox)
muutuja=get(h_popup,'String');
muutuja_indeks=get(h_popup,'Value');
valitudkuu = muutuja{muutuja_indeks};
[z kuupaevad]=xlsread('Andmed.xlsx',valitudkuu,'A:A');
global vk
vk = valitudkuu;
kuupaevad(1,:) = [];
set(h_listbox,'String',kuupaevad)
end
%=============== Checkbox kaotab vajadusel popupmenüü ja taastab selle,
%lisaks vahetab listboxi valikud kuupäevade ja kuude vahel =============
function checkbox_callback(obj,syndmus,h_listbox,h_popup)
global kuud
valitud_objekt=get(obj,'Value');
if valitud_objekt == 0
h_popup.Visible = 'On';
set(h_listbox,'String','')
elseif valitud_objekt == 1
set(h_listbox,'String',kuud)
set(h_listbox,'Value',1)
h_popup.Visible = 'Off';
else
disp('siia ei tohi jõuda')
end
end
%============== lsitboxi kaudu toimub põhiline andmetöötlus ja plottimine,
% vastavalt sellele, kas on valitud kuupäevad või kuud =====
function lst_Callback(obj, syndmus, h_polaraxes, h_radio_1, pealkiri, jaam)
sisu = cellstr(get(obj,'String'));
if size(sisu) == [12 1]
vkd=sisu(get(obj,'Value'));
raadiovaartus = get(h_radio_1,'Value');
if isempty(vkd)
disp('palun vali kuu')
else
suurus = size(vkd,1);
[~,jaamstr] = xlsread('Andmed.xlsx','Jaanuar','Z2');
set(jaam,'String',jaamstr);
if suurus < 5
set(pealkiri,'String',vkd + "-2022")
elseif suurus == 12
nn = "Histogramm - 2022";
set(pealkiri,'String',nn)
else
n = "Histogramm - " + num2str(suurus) + " kuud";
set(pealkiri,'String',n)
end
muut = [];
for i = 1:suurus
str = vkd{i};
asm = xlsread('Andmed.xlsx',str,'B:Y');
asm = deg2rad(asm);
muut = [muut,asm];
if raadiovaartus == 1
hist = polarhistogram(h_polaraxes,muut,16);
h_polaraxes.ThetaZeroLocation='Top';
h_polaraxes.ThetaTick=[0 45 90 135 180 225 270 315];
h_polaraxes.ThetaDir='clockwise';
h_polaraxes.ThetaTickLabel={'N' 'NE' 'E' 'SE' 'S' 'SW' 'W' 'NW'};
else
hist = polarhistogram(h_polaraxes,muut,32);
h_polaraxes.ThetaZeroLocation='Top';
h_polaraxes.ThetaTick=[0 22.5 45 67.5 90 112.5 135 157.5 180 202.5 225 247.5 270 292.5 315 337.5];
h_polaraxes.ThetaDir='clockwise';
h_polaraxes.ThetaTickLabel={'N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW'};
end
end
end
else
global vk
kpx = get(obj,'String');
ss = size(kpx,1);
valitud_kuupaevad=sisu(get(obj,'Value'));
raadiovaartus = get(h_radio_1,'Value');
if isempty(valitud_kuupaevad)
else
vkp1 = string(valitud_kuupaevad);
vkp2 = datestr(datenum(vkp1,'dd.mm.yyyy'),'dd mm yyyy');
paev = day(vkp2,'dd') + 1;
xlpaev = num2cell(paev);
suurus = size(xlpaev,1);
[~,jaamstr] = xlsread('Andmed.xlsx',vk,'Z2');
set(jaam,'String',jaamstr);
if suurus < 5
set(pealkiri,'String',vkp1)
elseif suurus == ss
nn = "Histogramm - " + vk + " 2022";
set(pealkiri,'String',nn)
else
n = "Histogramm - " + num2str(suurus) + " päeva";
set(pealkiri,'String',n)
end
muut = [];
for i = 1:suurus
num = xlpaev{i:i};
str = num2str(num);
xlstr = "B" + str + ":Y" + str;
asm = xlsread('Andmed.xlsx',vk,xlstr);
asm = deg2rad(asm);
muut = [muut,asm];
end
if raadiovaartus == 1
hist = polarhistogram(h_polaraxes,muut,16);
h_polaraxes.ThetaZeroLocation='Top';
h_polaraxes.ThetaTick=[0 45 90 135 180 225 270 315];
h_polaraxes.ThetaDir='clockwise';
h_polaraxes.ThetaTickLabel={'N' 'NE' 'E' 'SE' 'S' 'SW' 'W' 'NW'};
else
hist = polarhistogram(h_polaraxes,muut,32);
h_polaraxes.ThetaZeroLocation='Top';
h_polaraxes.ThetaTick=[0 22.5 45 67.5 90 112.5 135 157.5 180 202.5 225 247.5 270 292.5 315 337.5];
h_polaraxes.ThetaDir='clockwise';
h_polaraxes.ThetaTickLabel={'N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW'};
end
end
end
end
% =========== Raadionuppude muutmisvalikud ~~~~~~~~~
function radio_ryhm_callback(obj, syndmus, h_polaraxes)
valitud_objekt=get(get(obj,'SelectedObject'),'Tag');
switch valitud_objekt
case 'radio_1'
h_polaraxes.ThetaZeroLocation='Top';
h_polaraxes.ThetaTick=[0 45 90 135 180 225 270 315];
h_polaraxes.ThetaDir='clockwise';
h_polaraxes.ThetaTickLabel={'N' 'NE' 'E' 'SE' 'S' 'SW' 'W' 'NW'};
h_polaraxes.Children.NumBins = 16;
case 'radio_2'
h_polaraxes.ThetaZeroLocation='Top';
h_polaraxes.ThetaTick=[0 22.5 45 67.5 90 112.5 135 157.5 180 202.5 225 247.5 270 292.5 315 337.5];
h_polaraxes.ThetaDir='clockwise';
h_polaraxes.ThetaTickLabel={'N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW'};
h_polaraxes.Children.NumBins = 32;
otherwise
disp('Siia ei tohi kunagi jõuda!');
end
end
  1 个评论
Image Analyst
Image Analyst 2023-1-7
I get the error:
File "Kompass.png.png" does not exist.
Please attach that file with the paperclip icon so we can run your code.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

请先登录,再进行评论。

回答(1 个)

Dinesh
Dinesh 2023-3-6
Hi Rihard.
Since you have not attached any files, I can't run your code by myself since there are input files.
But here are some reasons why the code might be running slowly:
  1. Your code has input files and those files can have a large amount of data which could be the reason as to why your code is running very slowly.
  2. If your code uses a lot of memory, it can slow down the computation time.
  3. Since there is no context mentioned for your code, it is complicated to understand what you code is trying to do. So, the code might not be written keeping efficiency in mind and it has to be optimized in order to run faster.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by