On 1/2/2012 1:21 PM, Ray wrote:
> dpb <
no...@non.net> wrote in message <jdstj9$jqp$
1...@speranza.aioe.org>...
>> Where did this "mentioned file" come from? Is it externally generated?
>
> First of all, thanks for the very useful help and input ;)
> It is a matrix (and the other one is an array) filled with
> molefractions, energy values and molar weights, all put in vectors of 8
> elements. ...
> thought it would be better to precalculate all possible output values
> for all possible input values so that the answers would only have to be
> retrieved from a matlab datafile (an array, a mat file, a binary file,
> whatever is the fastest).
>
>> If so, and you're adamant about returning it by row record, write it
>> that way in the first place--it's the only sane way to attack the
>> problem.
> Sorry, but what exactly do you mean by 'writing something in a row
> record'? I thought making it a matrix was the most 'rowy' it can get? Or
> are you referring to the array?
I'm talking about your original post wherein you wrote a matrix (which
is column major) then tried to read it in row-major order by
manipulating offsets within the file.
You noted you could have transposed the matrix on write to put it in
that order but for some reason didn't want to. The point is, if you're
adamant about retrieving it in that order in pieces, the only possible
solution w/ any chance of any performance at all is to output it in the
same order in which you will want to read it.
>
>> But, even if that individual record is returned that way, it will have
>> poor overall performance because you'll be returning tiny little more
>> or less random sections of the file and running the disk heads all
>> over creation on every access, wiping cache and just killing any
>> chance of decent throughput.
> Okay, I see your point.
OK, beginning to get over our "failure to communicate"... :)
>> As IA says, read the file and do the retrieval in memory instead.
> Actually, I have tried this already but failed..
...
> ... I can't get a
> function to put something into the memory the first time so that it is
> still available the next time the same function is called. The function
> puts the thing into the memory, does its thing and then forgets
> everything when the function ends. I have used 'global' and 'persistent'
> commands, but nothing works. I really do not know of a way to make
> something stick into the base memory from within a called function
> (whithout ever using the command line or something).
...
> function loadC
if exist('C','var') == 0,
disp('C not loaded into base memory yet!');
global C;
load arrayC.mat % just a file with data
end
>
> % do something funky with the data
> C{1,3}+2
>
> end
>
> The variable C never shows up in the workspace.
Of course not, Matlab (like most modern languages) has function scope
and lifetime.
You haven't created a global variable C in the workspace if this is all
you have done. That would be one solution altho and one place where
global data wouldn't be all bad but it's still not what I'd choose as
the first solution.
In the main routine when you start the Matlab program just do
load arrayC.mat;
and you will have the data in the base workspace. Then, assuming the
above .mat file contains only an array 'C' that will be the name and
when you call whatever functions you need from the main script, simply
pass C to those functions which need it. Matlab is smart and will not
make needless copies so there's no additional memory or performance
issues to worry about.
The alternative is in the main routine to do sorta' as you did above and say
global C
load arrayC.mat;
then add global c into each function that needs access to it. You begin
to see already the problems global raises; you have to keep dragging the
definitions around to ensure you don't have a local variable C instead
of the global one and if you accidently do something to it in one of
these functions you've done it everywhere making debugging tough.
All in all, I'd recommend the non-global solution.
--