0

I needed to create a variable that have a dynamic name, and assign values to it. Since eval is bad practice, I'm using the structure method. However, it's still reducing the performnace very badly.

hf=figure;
createnewvar=1; %this value will be changing depends on external factor
tempvar=zeros(500,800,3,500);
varname='var1'; %this value will be changing depends on external factor

idx=1;
while ishandle(hf)

    if createnewvar
        Main.(varname)=zeros(500,800,3,500); 
        createnewvar=0;
    end

    somevalue=rand(500,800,3);
    tic
    tempvar(:,:,:,idx)=somevalue; %This line is the bottleneck
    tempvar_t=toc
    tic
    Main.(varname)=tempvar; %This line too
    Main_t=toc
    idx=idx+1;

end

And the time taken:

tempvar_t =

    2.5747


Main_t =

    0.5584

Any better solution to handle this? Thanks!

Gregor Isack
  • 1,111
  • 12
  • 25
  • If the bottleneck comes from assigning value to your dynamically named 'variable', dynamic naming isn't the issue then? Regardless, I wonder if the bottleneck is avoided if you don't have to name a temporary variable, which I don't really know how. But it may be worth using object instead to make sure the temp variable is passed by reference. That way, you are not doing extra copying at least. – Argyll Aug 07 '19 at 09:38
  • In any case, for me to know that getting around the extra copying will actually provide a solution to your question, I need to be able to rely on your determination of the bottleneck. Could you demonstrate that in your question? – Argyll Aug 07 '19 at 10:22
  • 2
    In your loop, you only modify the first 3 dimensions of `tempvar`, for a given slice `idx` of the 4th dimension. Yet on the next line you assign the full `tempvar` (the 500 slices of it) to the `Main.var1` ... it seems overkill as each iteration also store all the other iterations ... That is if you were iterating, the variable `idx` is never incremented here so your code is not complete, the intent is not understandable, so optimizing it is useless until it actually does what it is supposed to do – Hoki Aug 07 '19 at 10:26
  • @Argyll you're right, it isn't about the dynamic naming after all, see my edited questions. It seems like the assignment of value is the culprit. I don't see how to add into the main variable without using temporary var. @Hoki Sorry, I missed out the increment line code. You're right about the assignment of `tempvar` into `Main.var` that slowed down the performance. Beside using this method, I can't figure out any alternative. Any idea? – Gregor Isack Aug 07 '19 at 13:52
  • You do not need to use a temporary variable `tempvar` and perform the assignment twice. You can simply do: `Main.(varname)(:,:,:,idx)=rand(500,800,3);` – Hoki Aug 07 '19 at 14:49
  • I still don't understand what is the intent of the code and why you chose this construct, how many variable like `var1` do you anticipate ? – Hoki Aug 07 '19 at 14:50
  • @GregorIsack: Actually, I forgot that the [error ()-indexing must appear last in an index expression](https://stackoverflow.com/questions/29042046/is-there-any-way-around-error-indexing-must-appear-last-in-an-index-expressi) doesn't apply to dynamic field names of `struct` as long as it isn't call indirectly. So it seems you can avoid extra copying by just not creating the extra `tempvar`. – Argyll Aug 08 '19 at 07:29

0 Answers0