Interpolate strings from an x y interval to a corresponding x y interval.

I am restructuring a database and I have two tables in different formats but with the same information. Table One is a combination of values and strings where the from-to data are in custom intervals rounded to the nearest '0.25' as follows:
.
Table two is the same range of values and contains all the same integers but is formatted as fixed incremental 'blocks' instead of custom intervals.
I want to take the strings from table 1 and interp them onto table 2 so that if the 'from-to' values of table 2 falls within the range of an interval in table 1 it will have the corresponding strings as imagined below.
I can not seem to find a function that will allow me to perform this task with strings.
Any help would would be very much appreciated, thanks!

4 Comments

It would be much easier to suggest a solution, if you post some code to produce the inputs. Currently trying to create some code requires to invent some code for the inputs also, what is a waste of time, because you do have the data already. Please share them with us.
Roger. Sorry about that I'm new.
load intervals
intervalfrom = intervals(:,1);
intervalto = intervals(:,2);
string1 = intervals(:,3);
string2= intervals(:,4);
load blocks
blockfrom = blocks(:,1);
blockto = blocks(:,2);
x = interp2(blockfrom,blockto,string1,intervalfrom,intervalto);
Hello @Peter Wray and @Jan
Can you share the code of the solution because I have the same question and not able to find answers anywhere else on this topic ?
@Jay: But I did post the code in my answer already?! I suggest to open a new question to discuss the details of your problem.

Sign in to comment.

 Accepted Answer

The solution is not to do this with strings directly, but with indices:
idx = interp1(table1{:, 1}, 1:height(table1), table2{:, 1});
Now use this index to fill the column of table2.
One problem is, that e.g. the 0.25 appears at final point of one interval and the start point of the next one. To which of the two does 0.25 belong?

3 Comments

Good point. I restructured the data to avoid this issue. How do I use an index to fill the column? Thanks again!
load intervals
intervalfrom = intervals(:,1);
intervalto = intervals(:,2);
string1 = intervals(:,3);
string2= intervals(:,4);
load blocks
blockfrom = blocks(:,1);
blockto = blocks(:,2);
blockto = table2array(blockto);
blockto = blockto - 0.01;
blockto = array2table(blockto);
intervalto = table2array(intervalto);
intervalto = intervalto - 0.01;
intervalto = array2table(intervalto);
intervalstable = [intervalfrom,intervalto,string1,string2];
blockstable = [blockfrom,blockto];
idx = interp1(intervalstable{:, 1}, 1:height(intervalstable), blockstable{:, 1});
This is failing:
load intervals
intervalfrom = intervals(:,1);
The MAT file intervals.mat contains a variable called lithdataint. blocks.mat contains a variable blocksft. Should I convert the names "intervals" and "blocks" accordingly?
I'd expect a simple solution as:
table2(:, 3) = table1(idx, 3)
Hello, yes you should, I saved it as a matlab table under the names intervals but didn't change the name with code.
I rearanged my data base so it would index correctly (ran into a few issues after the inital one you pointed out), and then ran a fix on the index so it was all positive integers then used your code to add over the strings. Thank you so much and sorry for the bad formating. I'll do much better next time.

Sign in to comment.

More Answers (1)

If you had timetables, ths would be a one-liner using synchronize. from and blocksFrom look suspiciously like time vectors. Timetables might make your life easier, but hard to tell.

Products

Release

R2021b

Asked:

on 6 Mar 2022

Commented:

Jan
on 15 Mar 2023

Community Treasure Hunt

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

Start Hunting!