Only read data once (with xlsread)

Hi there,
I am using xlsread to import data from Excel. This takes quite some time, but that's ok. The real problem is that Matlab loads the data whenever I run the script. Hence, whenever I change a few variables and run the whole script, I have to wait minutes because matlab is working on re-loading the data using xlsread.
Is there any way to only load the data ONCE and not every time I run the whole script?
Any help is much appreciated. Thank you very much.
Best Christian

 Respuesta aceptada

Star Strider
Star Strider el 3 de Mayo de 2015

1 voto

If the Excel file never changes, one option is to save the data in it as a .mat file. Those are smaller to store and faster to load.
Another option is to create your script as a function and store your Excel file as a persistent variable, then calling your function from a new external script file. Running it as a function instead of a script has disadvantages as well, so consider those and decide if that is an acceptable tradeoff for loading your Excel file each time.

6 comentarios

Christian
Christian el 3 de Mayo de 2015
I tried saving it in a .mat file - wasn't able to load it from there.
Also tried using a function with the persistent variable. Didn't work either.
Here is one of the xlsread lines:
[prices,text] = xlsread('data.xlsx','Prices');
prices is a 2001x2234 double.
Can somebody help me with the code for either option suggested by Star Strider?
DON'T USE text AS THE NAME OF A VARIABLE. IT'S THE NAME OF A BUILT IN FUNCTION. Use XLText or something.
Are you're saying that
save(matFileName, 'prices', 'text');
to save it to disk, and then
s = load(matFileName);
process = s.prices;
xlText = s.XLText;
to recall it does not work???? Can you show anything to demonstrate that?
Christian
Christian el 3 de Mayo de 2015
Thanks! No I am not saying that it doesn't work. I'm saying I didn't know how to make it work ;-) I'm not very experienced with Matlab..
I will try your code right away!
You didn’t post your code with respect to the .mat file, so I can’t suggest what might be wrong with it.
Use the save command to save it and the load or matfile command to load it.
I would do something like this for the .mat file:
filename = 'XLS_File.mat';
save(filename, 'prices', 'text');
then use:
filename = 'XLS_File.mat';
load(filename)
to load all your variables from 'XLS_File.mat' into memory. The functions have options that are worth exploring to increase their functionality.
If the data in the file never changes, you can create a separate short script to read the Excel file once and save it to the .mat file. Then you just load it each time your main script runs.
Christian
Christian el 3 de Mayo de 2015
Wow, works like a charm! :-) Star Strider, you just reduced the data loading time from 5 minutes to 45 seconds :-)) Thank you very much!
Star Strider
Star Strider el 3 de Mayo de 2015
My pleasure!
The .mat files are binary files, so they’re smaller that other file types with the same information, and also load variables into your workspace faster than text or other format files.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 3 de Mayo de 2015

2 votos

Yes. You can make a GUI with two buttons and a few other controls to change variables (input parameters). One button will run a function that loads the data. Then the other one will get the various settings of your input parameters and run the analysis code.
Here is a good framework to get you started: MAGIC
Also go over the Mathworks video tutorial on using GUIDE: http://blogs.mathworks.com/videos/category/gui-or-guide/

6 comentarios

Christian
Christian el 3 de Mayo de 2015
Thank you very much for the quick reply! Unfortunately, however, I don't have the time right now to read up on GUI. Any chance there could be a simpler solution? E.g. outsourcing xlsread to a function or something like that?
Image Analyst
Image Analyst el 3 de Mayo de 2015
Try this:
>http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F>
Or else save your variable into a mat file with save() - thi smay be quicker to get back into your other functions than launching Excel.
Image Analyst
Image Analyst el 3 de Mayo de 2015
By the way, that reminds me of something I heard long ago while working for the government: "There's always time to do it over, but there's never time to do it right in the first place."
Christian
Christian el 3 de Mayo de 2015
True that :-)
Thanks for all your input!
Image Analyst
Image Analyst el 3 de Mayo de 2015
Glad my/our mat file suggestion (3 comments up) worked for you. Though you can only Accept one, perhaps you could also "Vote" for my answer too.
Christian
Christian el 4 de Mayo de 2015
Couldn't have done it without you, Image Analyst :-) I'm fascinated by Matlab and it's incredibly vibrant forum. It took you guys 10 minutes to solve my problem. A problem, I've been struggling with for days!
Thanks again for your help, really appreciate it!

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 3 de Mayo de 2015

Comentada:

el 4 de Mayo de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by