Matlab function works very slow












1














Extrapolating from a .mat file of the vectors x, y, z I have seen that my code is very slow in the operation and I can not understand why, even though I have done a lot of evidence about it.
The .mat file is initially composed of a 1x1 struct to which I access (in the code is: fileAllData{i,1}), a 2x1 cell to which I access (in the code: (attivita{1,j})) and finally the final cell to which I access ({s,1}).



function[x, y, z, s] = getXYZ(pathData, j, i, s)
fullDataPath = sprintf(...);
fileAllData = importdata(fullDataPath);
m = fileAllData{i,1}.(attivita{1,j}){s,1};

x = m(1,:)';
y = m(2,:)';
z = m(3,:)';
.....
s = s+1;

end


Main function:
.....
for j = 1:K
for i = 1:nSubjects
s = 1;
while s<=trials(j)

[x, y, z, s] = getXYZ(pathData, j, i, s);
end
end
end
...


What can be attributed to slowness? I do not know how to access data more efficiently. What can I try?










share|improve this question


















  • 2




    Have you tried profiling this function? You can do that by setting up a script which calls the function, and pressing "Run and Time" in the top bar. That'll tell you which parts of your function are the most time consuming. Run that, then edit the question to contain the result and investigate from there.
    – Adriaan
    Nov 22 at 14:31










  • Second the suggestion on profiling, also - how much total data is there? Would it make more sense to read the file once and get the data in memory, then process it?
    – paisanco
    Nov 22 at 14:32










  • I solve the problem with the suggestion of Adriaan, thank you very very much
    – Sara Savio
    Nov 22 at 14:46










  • Sara, if you solved the problem, please post an answer here, and accept it. Thanks!
    – Cris Luengo
    Nov 22 at 19:09










  • I bet your problem is in importdata. I’d suggest reading in the data only once, reading data is expensive.
    – Cris Luengo
    Nov 22 at 19:10
















1














Extrapolating from a .mat file of the vectors x, y, z I have seen that my code is very slow in the operation and I can not understand why, even though I have done a lot of evidence about it.
The .mat file is initially composed of a 1x1 struct to which I access (in the code is: fileAllData{i,1}), a 2x1 cell to which I access (in the code: (attivita{1,j})) and finally the final cell to which I access ({s,1}).



function[x, y, z, s] = getXYZ(pathData, j, i, s)
fullDataPath = sprintf(...);
fileAllData = importdata(fullDataPath);
m = fileAllData{i,1}.(attivita{1,j}){s,1};

x = m(1,:)';
y = m(2,:)';
z = m(3,:)';
.....
s = s+1;

end


Main function:
.....
for j = 1:K
for i = 1:nSubjects
s = 1;
while s<=trials(j)

[x, y, z, s] = getXYZ(pathData, j, i, s);
end
end
end
...


What can be attributed to slowness? I do not know how to access data more efficiently. What can I try?










share|improve this question


















  • 2




    Have you tried profiling this function? You can do that by setting up a script which calls the function, and pressing "Run and Time" in the top bar. That'll tell you which parts of your function are the most time consuming. Run that, then edit the question to contain the result and investigate from there.
    – Adriaan
    Nov 22 at 14:31










  • Second the suggestion on profiling, also - how much total data is there? Would it make more sense to read the file once and get the data in memory, then process it?
    – paisanco
    Nov 22 at 14:32










  • I solve the problem with the suggestion of Adriaan, thank you very very much
    – Sara Savio
    Nov 22 at 14:46










  • Sara, if you solved the problem, please post an answer here, and accept it. Thanks!
    – Cris Luengo
    Nov 22 at 19:09










  • I bet your problem is in importdata. I’d suggest reading in the data only once, reading data is expensive.
    – Cris Luengo
    Nov 22 at 19:10














1












1








1







Extrapolating from a .mat file of the vectors x, y, z I have seen that my code is very slow in the operation and I can not understand why, even though I have done a lot of evidence about it.
The .mat file is initially composed of a 1x1 struct to which I access (in the code is: fileAllData{i,1}), a 2x1 cell to which I access (in the code: (attivita{1,j})) and finally the final cell to which I access ({s,1}).



function[x, y, z, s] = getXYZ(pathData, j, i, s)
fullDataPath = sprintf(...);
fileAllData = importdata(fullDataPath);
m = fileAllData{i,1}.(attivita{1,j}){s,1};

x = m(1,:)';
y = m(2,:)';
z = m(3,:)';
.....
s = s+1;

end


Main function:
.....
for j = 1:K
for i = 1:nSubjects
s = 1;
while s<=trials(j)

[x, y, z, s] = getXYZ(pathData, j, i, s);
end
end
end
...


What can be attributed to slowness? I do not know how to access data more efficiently. What can I try?










share|improve this question













Extrapolating from a .mat file of the vectors x, y, z I have seen that my code is very slow in the operation and I can not understand why, even though I have done a lot of evidence about it.
The .mat file is initially composed of a 1x1 struct to which I access (in the code is: fileAllData{i,1}), a 2x1 cell to which I access (in the code: (attivita{1,j})) and finally the final cell to which I access ({s,1}).



function[x, y, z, s] = getXYZ(pathData, j, i, s)
fullDataPath = sprintf(...);
fileAllData = importdata(fullDataPath);
m = fileAllData{i,1}.(attivita{1,j}){s,1};

x = m(1,:)';
y = m(2,:)';
z = m(3,:)';
.....
s = s+1;

end


Main function:
.....
for j = 1:K
for i = 1:nSubjects
s = 1;
while s<=trials(j)

[x, y, z, s] = getXYZ(pathData, j, i, s);
end
end
end
...


What can be attributed to slowness? I do not know how to access data more efficiently. What can I try?







matlab performance function time






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 14:24









Sara Savio

146




146








  • 2




    Have you tried profiling this function? You can do that by setting up a script which calls the function, and pressing "Run and Time" in the top bar. That'll tell you which parts of your function are the most time consuming. Run that, then edit the question to contain the result and investigate from there.
    – Adriaan
    Nov 22 at 14:31










  • Second the suggestion on profiling, also - how much total data is there? Would it make more sense to read the file once and get the data in memory, then process it?
    – paisanco
    Nov 22 at 14:32










  • I solve the problem with the suggestion of Adriaan, thank you very very much
    – Sara Savio
    Nov 22 at 14:46










  • Sara, if you solved the problem, please post an answer here, and accept it. Thanks!
    – Cris Luengo
    Nov 22 at 19:09










  • I bet your problem is in importdata. I’d suggest reading in the data only once, reading data is expensive.
    – Cris Luengo
    Nov 22 at 19:10














  • 2




    Have you tried profiling this function? You can do that by setting up a script which calls the function, and pressing "Run and Time" in the top bar. That'll tell you which parts of your function are the most time consuming. Run that, then edit the question to contain the result and investigate from there.
    – Adriaan
    Nov 22 at 14:31










  • Second the suggestion on profiling, also - how much total data is there? Would it make more sense to read the file once and get the data in memory, then process it?
    – paisanco
    Nov 22 at 14:32










  • I solve the problem with the suggestion of Adriaan, thank you very very much
    – Sara Savio
    Nov 22 at 14:46










  • Sara, if you solved the problem, please post an answer here, and accept it. Thanks!
    – Cris Luengo
    Nov 22 at 19:09










  • I bet your problem is in importdata. I’d suggest reading in the data only once, reading data is expensive.
    – Cris Luengo
    Nov 22 at 19:10








2




2




Have you tried profiling this function? You can do that by setting up a script which calls the function, and pressing "Run and Time" in the top bar. That'll tell you which parts of your function are the most time consuming. Run that, then edit the question to contain the result and investigate from there.
– Adriaan
Nov 22 at 14:31




Have you tried profiling this function? You can do that by setting up a script which calls the function, and pressing "Run and Time" in the top bar. That'll tell you which parts of your function are the most time consuming. Run that, then edit the question to contain the result and investigate from there.
– Adriaan
Nov 22 at 14:31












Second the suggestion on profiling, also - how much total data is there? Would it make more sense to read the file once and get the data in memory, then process it?
– paisanco
Nov 22 at 14:32




Second the suggestion on profiling, also - how much total data is there? Would it make more sense to read the file once and get the data in memory, then process it?
– paisanco
Nov 22 at 14:32












I solve the problem with the suggestion of Adriaan, thank you very very much
– Sara Savio
Nov 22 at 14:46




I solve the problem with the suggestion of Adriaan, thank you very very much
– Sara Savio
Nov 22 at 14:46












Sara, if you solved the problem, please post an answer here, and accept it. Thanks!
– Cris Luengo
Nov 22 at 19:09




Sara, if you solved the problem, please post an answer here, and accept it. Thanks!
– Cris Luengo
Nov 22 at 19:09












I bet your problem is in importdata. I’d suggest reading in the data only once, reading data is expensive.
– Cris Luengo
Nov 22 at 19:10




I bet your problem is in importdata. I’d suggest reading in the data only once, reading data is expensive.
– Cris Luengo
Nov 22 at 19:10

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433030%2fmatlab-function-works-very-slow%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433030%2fmatlab-function-works-very-slow%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...