polyfit with cell array with non-uniform dimensions

I have a 10000×1 cell array. This example could clarify my format of data:
Assume 3×1 cell array of this cell array as follow
input_data =
{'[700 900]' }
{'[700 900 1050 800]'}
{'[700]' }
I used this function to convert to double:
Current_mA=cellfun(@str2num,input_data,'UniformOutput',false);
and the output was like this:
{1×2 double}
{1×4 double}
{[ 700]}
or
{[700 900] }
{[700 800 1050 950]}
{[700] }
Voltage_V =
{[3.3 3.4] }
{[2.3 2.37 2.35 2.5]}
{[1.95] }
I want to use below vector to interpolate between data in voltage_V with "polyfit" function
input_current_mA =
750
600
800
approxPoly = polyfit(Current_mA/1000, Voltage_V, size_sample_V - 1);
input_voltage_V = polyval(approxPoly, input_current_mA/1000);
size_sample_V is the number of each data in row. For expample for above Voltage_V:
size_sample_V =
2
4
1
The answer should be:
input_voltage_V =
3.325
2.446
1.95
  1. My first question is how to create "size_sample_V" automatically? (size of each row in cell array)
  2. If I convert the Current_mA & Voltage_V to array with blanks converted to NaN, how to use "polyfit" function to avoid this answer in output for:
approxPoly =
NaN NaN
3. Can I use cell array to use polyfit?
Thank you in advance for helping.

 Accepted Answer

Let Current_mA, voltage_V be your cell arrays. They are of equal dimensions.
N = length(approxPoly) ;
for i = 1:N
approxPoly{i} = polyfit(Current_mA{i}/1000, Voltage_V{i}, length(Current_mA{i}) - 1);
input_voltage_V{i} = polyval(approxPoly{i}, input_current_mA{i}/1000);
end
Note that if you have only one data point in the cell array you cannot fit any polynomial.

More Answers (0)

Categories

Asked:

on 6 Mar 2022

Commented:

on 6 Mar 2022

Community Treasure Hunt

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

Start Hunting!